私はジャスミンでAngularJSのデータポーリングをテストする方法は?
コントローラロジックは、この
$onInit() {
this.requestRootLocations();
this.startDataPolling();
}
private startDataPolling() {
this.stop = this.$interval(() =>
this.requestRootLocations()
, this.DATA_POLLING_INTERVAL);
}
private requestRootLocations() {
this.locationsService.requestRootElements().then(function (locationItem) {
...
this.entityList = (locationItem["locations"] instanceof Array) ? locationItem["locations"] : [];
}.bind(this), function() {
console.log("Error on loading locations data.");
});
}
アップデートのように見える私のコントローラのテストデータのポーリングに悩みを持っている:サービスで httpリクエスト:
public requestRootElements() {
return this.$http.get(this.API + "locations").then((response) => response.data);
}
これは正常に動作します期待通り。
テストケースはこの
it("should call http request on intervals", function() {
ctrl.$onInit();
$httpBackend.whenGET(api + "locations").respond(200, locationData);
// advance in time by 1 seconds
$interval.flush(1000);
$httpBackend.expectGET(api + "locations");
// after 1 seconds locations should be called once
$httpBackend.flush();
expect(ctrl.counts.length).toBe(4);
expect(ctrl.entities.length).toBe(6);
$interval.flush(dataPollingInterval);
$httpBackend.expectGET(api + "locations"); //does not work, why?
$httpBackend.flush();
// after the second call the data should be the same in this case
expect(ctrl.counts.length).toBe(4);
expect(ctrl.entities.length).toBe(6);
});
のように見えますが、2番目expectGETにエラー
Error: Unsatisfied requests: GET /apiv1/locations
これらの$ httpリクエストはどこにありますか?切り詰められたコードスニペットを投稿することは役に立ちません。これがコントローラで発生した場合は、完全に投稿してください。これがサービスで発生した場合は、コントローラとサービスの両方を送信してください。 – estus
ありがとう、私は質問を更新しました – Juri
まだなぜそれが失敗するか分からない。 DATA_POLLING_INTERVALが1000であることは明らかではありません。実際の問題は、複数のユニットが1つの統合テストに混在し、それほど効果的ではないということです。あまりにも多くの可動部品を導入し、同時に孤立してテストすることができなかったものはテストしません。答えを見てください。 – estus