2015-02-06 107 views
1

當我試圖測試,它返回一個承諾,我得到以下錯誤的函數:茉莉2.0測試angularjs工廠方法返回一個承諾

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

我的規格如下:

describe('async promise test', function() { 

    beforeEach(module('app')); 

    beforeEach(function() { 
     jasmine.DEFAULT_TIMEOUT_INTERVAL = 6 * 1000; 
    }) 

    it('should match the name', function (done) { 
     inject(function ($rootScope,promiseTest) { 
      $rootScope.$apply(function(){ 
      var promise =promiseTest.checkPromise(); 
      promise.then(function(data){ 
      console.log(data); 
      done(); 
      }) 
     }) 
     }) 
    }) 
}); 

請檢查plunker鏈接的完整代碼plunker link

回答

0

您正在使用mock模塊,它將覆蓋$timeout,因此測試是可重複的(ref)!

你必須使用$timeout.flush(1000);(和$apply()是多餘的):

it('should match the name', function (done) { 
    inject(function (promiseTest,$timeout) { 
    var promise =promiseTest.checkPromise(); 
    promise.then(function(data){ 
     console.log(data); 
     done(); 
    }); 
    $timeout.flush(1000); 
    }); 
}); 

見分叉普拉克:http://plnkr.co/edit/J1EmU7yuCETBd8w9mS1R?p=preview

0

你必須注入$超時到您的測試,並進行沖洗:

inject(function ($rootScope,promiseTest, $timeout) { 
     $rootScope.$apply(function(){ 
     var promise =promiseTest.checkPromise(); 
     console.log("Created promise") 
     promise.then(function(data){ 
     console.log(data); 
     done(); 
     }) 
    }); 
    $timeout.flush(); 

查看修改後的重擊器:

http://plnkr.co/edit/psBl6nROkXHrD1iExKAQ?p=preview

+0

嘿卡爾,其實我試圖測試索引數據庫,它不按預期工作....鏈接:http://plnkr.co/edit/dSdapvqTgR7qPeZC0umT?p=preview – 2015-02-11 08:13:49

相關問題