gpt4 book ai didi

javascript - jasmine:在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时时间内未调用异步回调

转载 作者:行者123 更新时间:2023-12-03 10:06:24 25 4
gpt4 key购买 nike

我有一个名为 requestNotificationChannel 的 Angular 服务:

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
};

});

我正在尝试使用 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();
});
});

我阅读了 Jasmine 中的异步支持,但由于我对使用 javascript 进行单元测试比较陌生,因此无法使其工作。

我收到一个错误:
Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

我的测试执行时间太长(大约 5 秒)。

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

最佳答案

在您的 it 中争论函数(以下代码中的 done)将导致 Jasmine 尝试异步调用。

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

//this block signature will run synchronously
it("should work", function(){
//...
});
done 没有什么区别参数被命名,它的存在才是最重要的。我从太多的副本/面食中遇到了这个问题。

Jasmine Asynchronous Support文档注意到参数(上面命名为 done)是一个回调,可以调用它让 Jasmine 知道异步函数何时完成。如果你从不调用它,Jasmine 永远不会知道你的测试已经完成并且最终会超时。

关于javascript - jasmine:在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时时间内未调用异步回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28861550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com