angularjs – jasmine:在由jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时内未调用异步回调

前端之家收集整理的这篇文章主要介绍了angularjs – jasmine:在由jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时内未调用异步回调前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个角服务叫做requestNotificationChannel:
  1. app.factory("requestNotificationChannel",function($rootScope) {
  2.  
  3. var _DELETE_MESSAGE_ = "_DELETE_MESSAGE_";
  4.  
  5. function deleteMessage(id,index) {
  6. $rootScope.$broadcast(_DELETE_MESSAGE_,{ id: id,index: index });
  7. };
  8.  
  9. return {
  10. deleteMessage: deleteMessage
  11. };
  12.  
  13. });

我试图使用茉莉花单元测试这项服务:

  1. "use strict";
  2.  
  3. describe("Request Notification Channel",function() {
  4. var requestNotificationChannel,rootScope,scope;
  5.  
  6. beforeEach(function(_requestNotificationChannel_) {
  7. module("messageAppModule");
  8.  
  9. inject(function($injector,_requestNotificationChannel_) {
  10. rootScope = $injector.get("$rootScope");
  11. scope = rootScope.$new();
  12. requestNotificationChannel = _requestNotificationChannel_;
  13. })
  14.  
  15. spyOn(rootScope,'$broadcast');
  16. });
  17.  
  18.  
  19. it("should broadcast delete message notification",function(done) {
  20.  
  21. requestNotificationChannel.deleteMessage(1,4);
  22. expect(rootScope.$broadcast).toHaveBeenCalledWith("_DELETE_MESSAGE_",{ id: 1,index: 4 });
  23. done();
  24. });
  25. });

我阅读关于Jasmine的异步支持,但因为我是新的单元测试与JavaScript不能使它的工作。

我收到一个错误

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

和我的测试花了太长的时间(约5秒)。

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

在你的函数中有一个参数将导致它尝试异步调用
  1. //this block signature will trigger async behavior.
  2. it("should work",function(done){
  3. //...
  4. });
  5.  
  6. //this block signature will run synchronously
  7. it("should work",function(){
  8. //...
  9. });

它没有区别什么做的参数命名,它的存在就是重要的。我从太多的副本/面食碰到这个问题。

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

猜你在找的Angularjs相关文章