【问题标题】:asynchronous test jasmine timeout error异步测试茉莉超时错误
【发布时间】: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 代码(spyOnwindow.parent)移动到it,因为只有一个测试。跨度>
  • 我无法将 done 添加到描述中,我将收到“描述不期望完成参数”错误
  • 你不应该有这样的错误。见:jasmine.github.io/2.0/…
  • 我知道这就是我问这个的原因:)
  • 是的,我的错。它应该传递给it,而不是describe。 :-)

标签: javascript asynchronous jasmine


【解决方案1】:

最后我简化了我的测试。我使用 eventDispatcher 手动触发我的服务正在侦听并忘记 window.postMessage() 的事件。原因是我正在测试的是服务而不是通信,所以事件如何发生并不重要。

it('my test', function(){
    var event = new CustomEvent('message');
    event.origin = 'http://localhost:33333';
    event.data = {message: 'eventData'};
    spyOn(myService.$rootScope, '$broadcast');
    myService.subscribeEvent();
    window.dispatchEvent(event);
    expect(myService.$rootScope.$broadcast).toHaveBeenCalledWith('eventData',undefined);
});

【讨论】:

    猜你喜欢
    • 2018-01-23
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多