2017-01-03 5 views
1

に実行されないばかり決して私はこのジャスミンのテストを持っている、と私はエラーを取得するよう約束のfinally節は、実行取得されていないために表示されます。最後の約束の句はジャスミンテスト

PhantomJS 2.1.1 (Mac OS X 0.0.0) Service: petsFactory .getPetsAsync() should return a list of pets FAILED 
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. 

私のテストファイルのルックスを以下のような:

'use strict'; 

describe('Service: petsFactory', function() { 

    // load the service's module 
    beforeEach(module('smokeTestApp')); 

    // instantiate service 
    var petsFactory; 
    beforeEach(inject(function (_petsFactory_) { 
    petsFactory = _petsFactory_; 
    })); 

    describe('.getPetsAsync()', function() { 
    it('should return a list of pets', function (done) { 

     var testPets = function (pets) { 
     expect(Array.isArray(pets)).toBe(true); 
     } 

     var failTest = function(error) { 
     expect(error).toBeUndefined(); 
     }; 

     petsFactory 
     .getPetsAsync() 
     .then(testPets) 
     .catch(failTest) 
     .finally(done); 
    }); 
    }); 
}); 

関連するファクトリメソッドは次のようになります。

var getPetsAsync = function() { 
    return $q.when(pets); 
}; 

ペットの内容VARI有能なものは完全に同期していますが、約束は即座に存在する同期値の単なるラッパーに過ぎません。

ここで何が問題になりますか? documentationから

+0

'then'関数が呼び出されていますか?また、工場で$ qを普通に注入しますか?コントローラ/サービスの通常の注入を使用するのではなく、角度$のインジェクタに干渉していたので、私は過去に同様の問題を抱えていました。 –

+0

$ qは正常に注入されます。おそらく、呼び出されていない可能性があります。私はちょうどtestPets関数で例外を投げてみたが、まだジャスミンで同じエラーが発生しました。しかし、同期値をラップするときに使用しているように、それは本当に奇妙なことです。 –

答えて

1

When testing promises, it's important to know that the resolution of promises is tied to the digest cycle. That means a promise's then, catch and finally callback functions are only called after a digest has run. In tests, you can trigger a digest by calling a scope's $apply function. If you don't have a scope in your test, you can inject the $rootScope and call $apply on it. There is also an example of testing promises in the $q service documentation.

だから、単に$rootScopeを注入し、$applyを使用します。

petsFactory 
    .getPetsAsync() 
    .then(testPets) 
    .catch(failTest) 
    .finally(done); 

$rootScope.$apply();