私は、サーバーからデータを取得し、複数の要求を作成してオブザーバブルの配列を返すデータサービスを用意しています。私はそのデータをテストしたい。複数の呼び出しを行い、応答をマッピングせずにobservableを返すHTTPサービスのテスト
私は何をやってみたのかは、データをテストする正しい方法があるかどうかわかりませんが、2つの観測値を含む配列を送信しました。
しかし、テストでは、非同期テストブロック
重要で、特に最後の3つのテストを失敗している:観測可能に加入、間に合わないメソッドを呼び出し、falsyするfalsyとcomicsIdするcharIdを設定するとき、私は、それをテストしたいですhttpを嘲笑した後、2つの期待される応答を含む配列が返されます。 charIdが真実ならば、期待される4つの応答と同じです。 6つの期待される応答のための同じcomicsIdがtruthyある
//データをフェッチサービス
getChar(): Observable<any> {
const Observables = [];
Observables.push(this.http.get('https://gateway.marvel.com:443/v1/public/characters?apikey'));
Observables.push(this.http.get('https://gateway.marvel.com:443/v1/public/comics?apikey'));
if (this.charId) {
Observables.push(this.http.get(`${this.urlChar}${this.charId}${this.apiKey}`));
Observables.push(this.http.get(`${this.urlChar}${this.charId}/comics${this.apiKey}`));
}
if (this.comicsId) {
Observables.push(this.http.get(`${this.urlCom}${this.comicsId}${this.apiKey}`));
Observables.push(this.http.get(`${this.urlCom}${this.comicsId}/creators${this.apiKey}`));
}
console.log([Observable, Observable]);
return Observable.forkJoin(Observables);
}
}
//すべての私のテスト
import { async, ComponentFixture, TestBed, getTestBed, inject } from '@angular/core/testing';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { DataService } from './data.service';
import {
BaseRequestOptions, Http, XHRBackend, HttpModule,
Response, ResponseOptions, RequestMethod
} from '@angular/http';
import { Observable } from 'rxjs/Observable';
describe('DataService',() => {
let mockBackend: MockBackend;
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
DataService,
MockBackend,
BaseRequestOptions,
{
provide: Http,
deps: [MockBackend, BaseRequestOptions],
useFactory:
(backend: XHRBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
}
}
],
imports: [
HttpModule
]
});
mockBackend = getTestBed().get(MockBackend);
}));
it('should get ObservableArr', (done) => {
let dataService: DataService;
getTestBed().compileComponents().then(() => {
mockBackend.connections.subscribe(
(connection: MockConnection) => {
connection.mockRespond(new Response(
new ResponseOptions({
body: [Observable, Observable]
}
)));
});
dataService = getTestBed().get(DataService);
expect(DataService).toBeDefined();
dataService.getChar().subscribe((obsArr: Observable<any>[]) => {
expect(obsArr.length).toBeDefined();
expect(obsArr.length).toEqual(2);
expect(obsArr.length).not.toBe(1);
done();
});
});
});
it('should check the service',
inject([DataService], (service: DataService) => {
expect(service).toBeTruthy();
}));
it('should get ObservableArray async',
async(inject([DataService], (dataService: DataService) => {
mockBackend.connections.subscribe(
(connection: MockConnection) => {
connection.mockRespond(new Response(
new ResponseOptions({
body: [Observable, Observable]
}
)));
});
dataService.getChar().subscribe(
(response) => {
expect(response.length).toBe(2);
expect(response[0]).toBe(Observable); <<<<<<<<<<<<<< Fails
expect(response[1]).toBe(Observable); <<<<<<<<<<<<<< Fails
expect(response).toEqual([Observable, Observable]); <<<<<< Fails
});
})));
});
'forkJoin'がどのように動作するか誤解しているかもしれません。それはオブザーバブルを返さず、各コンポーネントオブザーバブルから実際の結果を返します。しかし、私はまだ答えを提供するために十分なテストに精通していないと言っている。 –
デバッグ用に最小のgit repoを提供できますか? –
はい私は分を与えることができます – SONGSTER