Jasmine Async Testing

删除回忆录丶 提交于 2019-12-11 07:49:26

问题


I'm attempting to test a value that is set asynchronously using Jasmine 2's new done() callback.

I've based my test after the example Jasmine gives in their documentation (http://jasmine.github.io/2.0/upgrading.html#section-Asynchronous_Specs):

it('can set a flag after a delay', function(done) {

  var flag = false,
  setFlag = function() {
    //set the flag after a delay
    setTimeout(function() {
        flag = true;
        done();
    }, 100);
  };

  setFlag();
  expect(flag).toBe(true);
});

I'm getting the result "Expected false to be true", so I'm guessing that it's not waiting for the done() callback to be invoked before checking the value of the flag.

Does anyone know why this test is failing?

Thanks!


回答1:


It's because you're running your assertion as soon as setTimeout has been call, thus you're not giving it enough time to invoke the callback that sets flag to true. The below code will work (run the below code at TryJasmine to see how it behaves):

describe('flag delays', function () {
  it('can set a flag after a delay', function(done) {
    var flag = false,
    setFlag = function() {
      //set the flag after a delay
      setTimeout(function() {
          flag = true;
          expect(flag).toBe(true);
          done();
      }, 100);
    };

    setFlag();
  });
});

Going forward, Jasmine has a waitsFor method to facilitate testing timers. Even better, Sinon.JS provides functionality for faking times, which enables to skip setTimeout invocations and verify any behaviour without creating duration-based dependencies in your tests. Additionally, you'll be able to write assertions at the end of your tests as your did in your question, which will massively improve readability.



来源:https://stackoverflow.com/questions/27828108/jasmine-async-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!