javascript - Get Frisby.js tests to run synchronously - Stack Overflow

I have these API calls in my test that need to run first so I can store the response in a variable to u

I have these API calls in my test that need to run first so I can store the response in a variable to use later. But it looks like my tests are running asynchronously so the second test finishes before the variable gets populated. How can I make the tests run synchronously?

I've heard that one way is to use before and passing the done callback. But I'm not sure how to do that with jasmine-node.

Example of test:

var dataID = '';
frisby.create('Get ID')
  .get(url)
  .expectStatus(200)
  .afterJSON(function(json) {
     dataID = json.id;
  })
.toss();

frisby.create('Get data with ID')
  .get(url, id)
  .expectStatus(200)
  .expectJSON({"id": dataID})
.toss();

EDIT:

So I tried doing my test like this and the done() callback doesn't seem to get called. (The test times out)

describe('API TEST', function() {
  beforeEach(function(done) {
    frisby.create('Get ID')
      .get(url)
      .expectStatus(200)
      .afterJSON(function(json) {
        dataID = json.id;
        done();  //?
      })
      .toss()
  });
  it('should work', function() {
    console.log('TEST');
  }); //"timed out after 5000 msec waiting for spec to plete"
});

I have these API calls in my test that need to run first so I can store the response in a variable to use later. But it looks like my tests are running asynchronously so the second test finishes before the variable gets populated. How can I make the tests run synchronously?

I've heard that one way is to use before and passing the done callback. But I'm not sure how to do that with jasmine-node.

Example of test:

var dataID = '';
frisby.create('Get ID')
  .get(url)
  .expectStatus(200)
  .afterJSON(function(json) {
     dataID = json.id;
  })
.toss();

frisby.create('Get data with ID')
  .get(url, id)
  .expectStatus(200)
  .expectJSON({"id": dataID})
.toss();

EDIT:

So I tried doing my test like this and the done() callback doesn't seem to get called. (The test times out)

describe('API TEST', function() {
  beforeEach(function(done) {
    frisby.create('Get ID')
      .get(url)
      .expectStatus(200)
      .afterJSON(function(json) {
        dataID = json.id;
        done();  //?
      })
      .toss()
  });
  it('should work', function() {
    console.log('TEST');
  }); //"timed out after 5000 msec waiting for spec to plete"
});
Share Improve this question edited Mar 7, 2016 at 15:59 cocoa asked Mar 4, 2016 at 18:26 cocoacocoa 3,9217 gold badges31 silver badges57 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

What I ended up doing was using the async library and doing .timeout(60000) on the actual frisby test like so:

async.series([
  function(cb) {
    frisby.create('Get ID')
      .get(url)
      .expectStatus(200)
      .afterJSON(function(json) {
        dataID = json.id;
        cb();
      })
      .toss();
  },
  function() {
     //other tests using id
  }
]);

Jasmine handles asynchronous tests by passing a special done parameter to the test function as an argument -- you have to call done (ie done()) when the asynchronous part finishes.

Here is an example test using done:

describe('my test', function() {
  it('pletes on done', function(done) {
    var a = 10;

    // this would normally be a call to the code under test
    setTimeout(function() {
      a = 20;
    }, 250);

    setTimeout(function() {
      expect(a).toEqual(20);
      done();
    }, 1000);
  });
});

In the case of frisby.js, it appears that asynchronous testing is surprisingly still an issue. See the issues on the github repo:

open frisby issues involving async

This is a little late but in case someone else might have the same question. You can nest the second test into the afterJson() of the first test like this to make sure it runs after the first one is done

frisby.create('Get ID')
  .get(url)
  .expectStatus(200)
  .afterJSON(function(json) {
     var dataID = json.id;
     frisby.create('Get data with ID')
        .get(url, id)
        .expectStatus(200)
        .expectJSON({"id": dataID})
    .toss()
  })
.toss();

I did smaller modification for me in this function but this works for adding node with escape character in xml

private void somefunctToReplaceTxtinExistingNode(Node tempNode, String texttobeSet){
    String newtext = "<" + tempNode.getNodeName() + ">" + texttobeSet + "</" + tempNode.getNodeName() + ">";
    // create of new temp document .node will be imported from this
    DocumentBuilderFactory dbf = ....
    DocumentBuilder builder = ..
    Document newDoc = builder.parse(new StringBufferInputStream(texttobeSet));

    Node nodeXmlWithEscapeChars = newDoc.getFirstChild();
    // Import the node in old doc
    Document document = tempNode.getOwnerDocument();
    Node impNode = document.importNode(nodeXmlWithEscapeChars, true);
    // lastly . delete old with 
    Node parent = tempNode.getParentNode();
    parent.removeChild(tempNode);
    parent.appendChild(impNode);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745315936a4622219.html

相关推荐

  • javascript - Get Frisby.js tests to run synchronously - Stack Overflow

    I have these API calls in my test that need to run first so I can store the response in a variable to u

    10小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信