【发布时间】:2017-06-10 11:35:58
【问题描述】:
我已经阅读并尝试了不同的方法来在 jasmine 中进行异步测试,但没有任何成功。 jasmine: Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL
Testing window.postMessage directive
Testing postMessage with Jasmine async doesn't work
我有以下代码,并且收到了以下输出。
在指定的超时时间内未调用异步回调 jasmine.DEFAULT_TIMEOUT_INTERVAL
describe('asynchronous tests', function () {
var value;
beforeEach(function (done) {
spyOn(myService.$rootScope, '$broadcast').and.callFake(function(){
done();
});
window.parent.postMessage({message:'event'}, '*');
});
it('Should support async execution test preparation and expectation', function(){
expect(myService.$rootScope.$broadcast).toHaveBeenCalled();
});
});
myService 在父 describe 函数中定义。据我了解,我的 beforeEach 应该等到 postMessage 被抛出后再继续执行,但我收到了超时错误。
谢谢
【问题讨论】:
-
你需要通过
done:describe('asynchronous tests', function(done){我会将整个beforeEach代码(spyOn和window.parent)移动到it,因为只有一个测试。跨度> -
我无法将 done 添加到描述中,我将收到“描述不期望完成参数”错误
-
你不应该有这样的错误。见:jasmine.github.io/2.0/…
-
我知道这就是我问这个的原因:)
-
是的,我的错。它应该传递给
it,而不是describe。 :-)
标签: javascript asynchronous jasmine