3

私はthis exampleに従っていました。ジャスミン。角度サービス。角度の約束。一緒に遊ぶ方法

我々持っているようにテストスイート:

describe('Basic Test Suite', function(){ 
    var DataService, httpBackend; 

    beforeEach(module('iorder')); 
    beforeEach(inject(
     function (_DataService_, $httpBackend) { 
      DataService = _DataService_; 
      httpBackend = $httpBackend; 
     } 
    )); 
    //And following test method: 
    it('should call data url ', function() { 
     var promise = DataService.getMyData(); 
     promise.then(function(result) { 
      console.log(result, promise); // Don't gets here 
     }).finally(function (res) { 
      console.log(res); // And this is also missed 
     }) 
    }) 
}); 

がどのように約束を返す、角度のサービスとジャスミン+カルマの作品を作るためには?

私はthis questionを見ましたが、テストケースで約束を使用しているようです。約束をテストすることではありません。

答えて

2

テストが非同期であるため、約束が解決されるのを待つようジャスミンに伝える必要があります。あなたは、あなたのテストにdoneパラメータを追加することによって、次の操作を行います、あなたはジャスミンをさせるされている試験方法にdoneを追加することにより

describe('Basic Test Suite', function(){ 
    var DataService, httpBackend; 

    beforeEach(module('iorder')); 
    beforeEach(inject(
     function (_DataService_, $httpBackend) { 
      DataService = _DataService_; 
      httpBackend = $httpBackend; 
     } 
    )); 
    //And following test method: 
    it('should call data url ', function (done) { 
     var promise = DataService.getMyData(); 
     promise.then(function(result) { 
      console.log(result, promise); // Don't gets here 
      done();//this is me telling jasmine that the test is ended 
     }).finally(function (res) { 
      console.log(res); // And this is also missed 
      //since done is only called in the `then` portion, the test will timeout if there was an error that by-passes the `then` 
     }); 
    }) 
}); 

それが非同期テストであることを知っていて、どちらかdoneが呼び出されるまで待機します、またはタイムアウト。私はたいていthendoneを呼び出し、タイムアウトに頼ってテストに失敗します。また、doneに何らかのエラーオブジェクトを呼び出してテストに失敗すると思われるので、catchで呼び出すことができます。

+0

ええ、関数に引数を渡して、非同期テストを作成する - 最も明確な方法:) Thx。 :) –

関連する問題