Tôi có một dịch vụ góc gọi là 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
};
});
Tôi đang cố gắng thử nghiệm dịch vụ này bằng hoa nhài:
"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();
});
});
Tôi đã đọc về Hỗ trợ không đồng bộ trong Jasmine, nhưng vì tôi chưa quen với thử nghiệm đơn vị với javascript nên không thể làm cho nó hoạt động.
Tôi đang nhận được một lỗi:
Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL
và thử nghiệm của tôi mất quá nhiều thời gian để thực hiện (khoảng 5s).
Ai đó có thể giúp tôi cung cấp ví dụ làm việc về mã của tôi với một số giải thích?
Jestmong đợi - rất phổ biến trong khi gỡ lỗi và mất một thời gian để kiểm tra các biến.
afterEachbước đang xóa cơ sở dữ liệu (sử dụng deleteManyphương thức). Việc thêm jest.setTimeout(30000);vào beforeAllphương thức dường như đã khắc phục điều này đối với tôi - Tôi đoán vì việc xóa cơ sở dữ liệu là một cuộc gọi mạng (trong điều kiện), đôi khi mất hơn 3 giây và ném.