jasmine:在由jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时期间内未调用异步回调 [英] jasmine: Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

查看:140
本文介绍了jasmine:在由jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时期间内未调用异步回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 requestNotificationChannel 的角度服务:

I have an angular service called requestNotificationChannel:

app.factory("requestNotificationChannel", function($rootScope) {

    var _DELETE_MESSAGE_ = "_DELETE_MESSAGE_";

    function deleteMessage(id, index) {
        $rootScope.$broadcast(_DELETE_MESSAGE_, { id: id, index: index });
    };

    return {
       deleteMessage: deleteMessage
    };

});

我正在尝试使用茉莉花单元测试此服务:

I am trying to unit test this service using jasmine:

"use strict";

describe("Request Notification Channel", function() {
    var requestNotificationChannel, rootScope, scope;

    beforeEach(function(_requestNotificationChannel_) {
        module("messageAppModule");

        inject(function($injector, _requestNotificationChannel_) {
            rootScope = $injector.get("$rootScope");
            scope = rootScope.$new();
            requestNotificationChannel = _requestNotificationChannel_;
        })

        spyOn(rootScope, '$broadcast');
    });


    it("should broadcast delete message notification", function(done) {

        requestNotificationChannel.deleteMessage(1, 4);
        expect(rootScope.$broadcast).toHaveBeenCalledWith("_DELETE_MESSAGE_", { id: 1, index: 4 });
        done();       
    });
});

我在茉莉花中阅读了异步支持,但是我相当新的使用javascript cann进行单元测试

I read about the Asynchronous Support in Jasmine, but as I am rather new to unit testing with javascript couldn't make it work.

我收到错误:

Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

,我的测试花费的时间太长执行(约5s)。

and my test is taking too long to execute (about 5s).

有人可以帮助我提供一些代码的工作示例吗?

Can somebody help me providing working example of my code with some explanation?

推荐答案

中有一个参数函数将导致它尝试异步调用。

Having an argument in your it function will cause it to attempt an async call.

//this block signature will trigger async behavior.
it("should work", function(done){
  //...
});

//this block signature will run synchronously
it("should work", function(){
  //...
});

它没有什么不同,完成参数被命名,其存在是重要的。我从太多的副本/面食中遇到这个问题。

It doesn't make a difference what the done argument is named, its existence is all that matters. I ran into this issue from too much copy/pasta.

Jasmin 异步支持文档注意,参数(命名为 done above)是一个回调,可以调用让Jasmine知道异步函数何时完成。如果你永远不会打电话,茉莉花永远不会知道你的测试已经完成,最终会超时。

The Jasmin Asynchronous Support docs note that argument (named done above) is a callback that can be called to let Jasmine know when an asynchronous function is complete. If you never call it, Jasmine will never know your test is done and will eventually timeout.

这篇关于jasmine:在由jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时期间内未调用异步回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆