1

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 complete"
});
4

4 回答 4

3

我最终做的是使用async图书馆并.timeout(60000)像这样进行实际的frisby测试:

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
  }
]);
于 2016-03-08T21:24:05.203 回答
2

Jasmine 通过将一个特殊done参数作为参数传递给测试函数来处理异步测试——您必须done()在异步部分完成时调用 done (即 )。

这是一个使用 done 的示例测试:

describe('my test', function() {
  it('completes 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);
  });
});

在 frisby.js 的情况下,异步测试似乎仍然是一个问题。请参阅 github repo 上的问题:

涉及异步的开放式 frisby 问题

于 2016-03-04T18:41:31.643 回答
0

这有点晚了,但以防其他人可能有同样的问题。您可以像这样将第二个测试嵌套到第afterJson()一个测试中,以确保它在第一个测试完成后运行

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();
于 2016-06-27T23:08:15.273 回答
0

我在此函数中为我做了较小的修改,但这适用于在 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);
}
于 2017-04-22T10:54:08.360 回答