【问题标题】:AngularJS $q.defer() with jasmine done(): promise.then function not getting calledAngularJS $q.defer() with jasmine done(): promise.then 函数没有被调用
【发布时间】:2016-08-03 22:54:32
【问题描述】:

有人可以帮我理解为什么这不起作用吗?我正在使用 Jasmine 的 both 函数来等待 promise.then 函数也被调用并做出断言。但它在被调用之前就超时了。

describe('greeting', function(){
    var greeting = 'hello';

    it('should call promise.then', function(done) {
        inject(function ($q) {
            var deferred = $q.defer();
            deferred.resolve(greeting);

            deferred.promise.then(function (greeting) {
                expect(greeting).toEqual('hello');
                done();
            });

        });
    });
});

then 函数未被调用,Jasmine 超时并显示以下消息:

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

【问题讨论】:

    标签: angularjs asynchronous jasmine


    【解决方案1】:

    确保您执行$scope.$apply 来触发角度摘要循环,如下所示:

    inject(function ($q, $rootScope) {
       var deferred = $q.defer();
       deferred.resolve(greeting);
    
       deferred.promise.then(function (greeting) {
          expect(greeting).toEqual('hello');
          done();
       });
       $rootScope.$apply();
    });
    

    应该解决问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-27
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 2019-02-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      相关资源
      最近更新 更多