2017-11-07 5 views
1

私はサービスを含むコンポーネントメソッドをテストしようとします。私はあなたの検索方法をテストするために私が何をしたかの例を示します。MockBackend私は混乱しています

これは、検索機能を含む私のコンポーネントである:

search(idntDocTp:any,idntDocNo:any){ 

this.providersService.searchForSupportingStaffList(idntDocTp,idntDocNo).subscribe(
     data => { 
     this.searchSupportingStaffData = data[0]; 
     if (this.searchSupportingStaffData != null) 
     { 
     this.activeIndex = 1; 

     switch (this.searchSupportingStaffData.IdntType) { 
      case 'nID': 
      this.identificDocNumSymbol = 'NID'; 
      break; 
      case 'passport': 
      this.identificDocNumSymbol = 'PAS'; 
      break; 
      case 'arc': 
      this.identificDocNumSymbol = 'ARC'; 
      break; 
     } 
     } else { 
     this.alertMessage = { severity: 'warn', summary: 'Warning Message', detail: 'No records were found. Please try again.' }; 
     this.alertMessagesSrv.pushAlert(this.alertMessage); 
     } 
}); 

これは、これは私が私のサービスは、(宣言)動作するかどうかを確認するためにやっているテストです

searchForSupportingStaffList(idntDocTp:any,idntDocNo:any): Observable<any> { 

    let costructUrl: string; 
    costructUrl = this.searchSupporting_staffUrl + '/?IdntType=' + idntDocTp + '&IdntNo=' + idntDocNo; 
    return this.http 
    .get(costructUrl, 
       { headers: this.headers } 
      ) 
    .map((res: any) => res.json().data); 
} 

私のサービスです。

beforeEach(() => { 
TestBed.configureTestingModule({ 
    imports: [ 
    ProviderManagementModule, 
    // HttpModule, 
    BrowserAnimationsModule 
], 
//declarations: [ SupportingStaffComponent ], 
providers: [ 
    AlertMessagesService, 
    ProvidersService, 
    MockBackend, 
    BaseRequestOptions, 
    { 
     provide: Http, 
     useFactory:(backend,options) => new Http(backend,options), 
     deps:[MockBackend,BaseRequestOptions] 
    } 
]}) 
//Get the MockBackend 
backend = TestBed.get(MockBackend); 
//Returns a service with the MockBackend so we can test with dummy responses 
service = TestBed.get(ProvidersService); 

fixture = TestBed.createComponent(SupportingStaffComponent); 
app = fixture.debugElement.componentInstance;}); 

そしてここでは、実際のテスト

です
it('return Search results and active index is changed', fakeAsync(() => { 
    let response = { 
     data: [ 
     {id:1, name:'DummyName', IdntType:'nID', IdntNo:'991212', age:'27', start_date:'12 Sep 2017', end_date:'',contact_number:'99457845'} 
    ] 
    } 


    backend.connections.subscribe(conn => { 
      conn.mockRespond(new Response(new ResponseOptions({ body: JSON.stringify(response) }))); 
     }); 
     service.searchForSupportingStaffList('dummyData','dummyData'); 
     tick(); 
     fixture.detectChanges(); 
     expect(fixture.componentInstance.activeIndex).toEqual(1); 
     expect(fixture.componentInstance.searchSupportingStaffData.name).toMatch('DummyName') 
     expect(app.data.length).toBe(1); 
     })); 

テストは正常に実行されます。サービス関数にどのようなパラメータを設定しても、モックデータは返されません。それが私が混乱している理由です。私は偽のデータをサービスに提供すると思っていました。間違ったパラメータで検索すると、サービスは何も返しませんが、これは当てはまりません。私が間違っていると誰かが私に説明することはできますか?さらに、私のコンポーネントから検索メソッドを実行しても、サービスはまだ同じ結果を得ています。テストがうまくいくかどうか、実際にどのように理解するのですか?

答えて

0

レスポンスをハードコードしたので、これは問題ありません。私はここで間違ったことはしていないと思います。サービスのテストは、戻り値ではなくサービスの外部APIをテストすることです。

+0

お返事ありがとうございます。しかし、私の方法が何をすべきかどうかをどうやってテストするのですか?例えばここでは、私のメソッドが実際にパラメータに基づいてデータを検索するかどうかをテストしたいです。 – apoellitsi

+0

あなたは歓迎です。パラメータに基づいてデータをテストするということは、単体テストの範囲外のバックエンドAPIまたは契約をテストすることを意味します。 –

関連する問題