我有一个角服务叫做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
- };
- });
我试图使用茉莉花单元测试这项服务:
- "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秒)。
有人可以帮助我提供我的代码的工作示例与一些解释?
在你的函数中有一个参数将导致它尝试异步调用。
- //this block signature will trigger async behavior.
- it("should work",function(done){
- //...
- });
- //this block signature will run synchronously
- it("should work",function(){
- //...
- });
它没有区别什么做的参数命名,它的存在就是重要的。我从太多的副本/面食碰到这个问题。
Jasmin Asynchronous Support文档注意到,参数(上面做的命名)是一个回调,可以调用它来让Jasmine知道异步函数何时完成。如果你从不调用它,Jasmine永远不会知道你的测试是完成,并将最终超时。