2017-08-02 6 views
2

現在、httpリクエストサービスを呼び出してサブスクリプションパート内で何かを実行する関数をテストしようとしています(関数を呼び出して変数を設定します)。これまでのところ私のアプローチは単に関数を呼び出すことで、リクエストサービスが自動的に呼び出されると思ったので、購読部分が実行されます。しかし、私はこれが動作していないので、これを行う方法ではないように感じます。Angular2 - 内部でhttpサービスを呼び出す関数をテストします。

私がテストしたい機能:

public trainBot() { 
    this.isTraining = true; 
    this.requestsService.trainModel(this.botId, false) 
     .subscribe(response => { 
     this.trainingStatus = this.trainingStatusMapping[response['status']]; 
     this.pollTrainingStatus(); 
     }); 
    } 

私のテスト今のところ(動作しません)。

it('should poll the training status',() => { 
    spyOn(component, 'pollTrainingStatus').and.callThrough(); 
    component.trainBot(); 
    fixture.detectChanges(); 
    expect(component.pollTrainingStatus).toHaveBeenCalled(); 
    }); 

だから、誰もが(.subscribe内のその部分をテストする方法を教えてくださいすることができます...一部

更新:?

誰かが私がのreturnValueを追加提案されているようと非同期に私のテスト彼らはまだ動作していないが、今そのように見える:。

it('should poll the training status', fakeAsync(() => { 
    component.trainBot(); 
    spyOn(service, 'trainModel').and.returnValue(Observable.of({'status': 'training'})); 
    spyOn(component, 'pollTrainingStatus').and.callThrough(); 
    fixture.detectChanges(); 
    tick(1); 
    expect(service.trainModel).toHaveBeenCalled(); 
    expect(component.pollTrainingStatus).toHaveBeenCalled(); 
    })); 

エラーは同じ

です
+0

受信したエラー、または失敗したテストの出力を共有できますか? – Kevin

+0

@Kevin "spy pollTrainingStatusが呼び出されると予想されました。"これはpollTrainingStatus()が呼び出されていないことを意味します – threxx

+0

この質問をチェックしてください:[subscribeメソッドのためのジャスミンを使ったangular2テスト](https://stackoverflow.com/questions/40080912/angular2-testing-using-jasmine-for-subscribe-method) 。その答えがあなたのために働くかどうか私に教えてください。 – Kevin

答えて

4

はまず、あなたは、メソッドtrainBotを実行する前に、あなたのスパイを作成する必要があります()。それはあなたのテストを修正するはずです。

it('should poll the training status',() => { 
    spyOn(service, 'trainModel').and.returnValue(Observable.of({'status': 'training'})); 
    spyOn(component, 'pollTrainingStatus'); 

    component.trainBot(); 

    expect(service.trainModel).toHaveBeenCalled(); 
    expect(component.pollTrainingStatus).toHaveBeenCalled(); 
})); 

ただし、テストの戦略についてお話しましょう。

サービスではなくコンポーネントをテストしているので、サービスメソッドの呼び出しを許可しないでください。

spyOn(service, 'trainModel').and.returnValue(Observable.of({'status': 'training'})); 

さらに、単体テストはできるだけ小さなものをテストする必要があります。あなたは実際にここでコールバックをテストしています。コールバックはおそらく無名の矢印関数の代わりに名前付きのメソッドでなければなりません。次に、他のテストでコールバックの機能をテストして検証できます。

public trainBot() { 
    this.isTraining = true; 
    this.requestsService.trainModel(this.botId, false) 
     .subscribe(response => this.onTrainbotSuccess(response)); 
} 

public onTrainbotSuccess(response) { 
    this.trainingStatus = this.trainingStatusMapping[response['status']]; 
    this.pollTrainingStatus(); 
} 

このテスト、あなたは今、私たちは、特に成功コールバックが何のためにテストを書くことができ、応答方法が

it('should call service.trainModel',() => { 
    spyOn(service, 'trainModel').and.returnValue(Observable.of({'status': 'training'})); 

    component.trainBot(); 

    expect(service.trainModel).toHaveBeenCalled(); 
}); 

it('should send success responses to onTrainbotSuccess()',() => { 
    spyOn(component, 'onTrainbotSuccess'); 
    spyOn(service, 'trainModel').and.returnValue(Observable.of({'status': 'training'})); 

    component.trainBot(); 

    expect(component.onTrainbotSuccess).toHaveBeenCalled(); 
}); 

呼ばなっていることをテストすることができます。

it('should poll the training status',() => { 
    spyOn(component, 'pollTrainingStatus'); 

    component.onTrainbotSuccess({'status': 'training'}); 

    expect(component.pollTrainingStatus).toHaveBeenCalled(); 
}); 
+0

ああ私はそれを得た!それははるかに理にかなっています!ありがとうございました! – threxx

0

subscribe()に渡される関数は非同期に呼び出されます。つまり、アサーションはthis.pollTrainingStatus()が呼び出される前に実行されます。 async() or fakeAsync() utility functionを使用する必要があります。 (fakeAsync()を使用している場合)のコードでは

は次のようになります。

import { fakeAsync } from '@angular/core/testing'; 

it('should poll the training status', fakeAsync(() => { 
    ... 
    expect(component.pollTrainingStatus).toHaveBeenCalled(); 
})); 
+0

それでも、まだ動作していない、同じテストの結果を追加しました – threxx

+0

[docs](https://angular.io/guide/testing#the-fakeasync-function)のように 'tick()'を追加しましたか? – buoto

+0

はい私はtick()とtick(1)でそれを試した – threxx

関連する問題