그래서 서비스를 포함하는 구성 요소 메소드를 테스트 해 봅니다. 내 검색 방법을 테스트하기 위해 내가 한 일의 예를 보여 드리겠습니다.MockBackend 혼란 스러워요
이 검색 기능을 포함하는 내 구성 요소입니다 : 이것은이 내가 확인하기 위해 일을 오전 시험이
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);
}
내 서비스입니다
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);
}
});
을 경우 내 서비스 작업 (선언) :
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);
}));
테스트가 성공적으로 실행됩니다. 어떤 매개 변수를 서비스 함수로 설정해도 여전히 모의 데이터를 반환하지 않습니다. 그래서 혼란 스럽습니다. 나는 내가 모의 (mock) 데이터로 서비스를 제공한다고 생각하고 잘못된 매개 변수로 검색하면 서비스가 아무것도 반환하지 않을 것이라고 생각했다. 그러나 이것은 사실이 아니다. 내가 잘못한 일을 누군가 설명 할 수 있습니까? 또한 내 구성 요소에서 검색 방법을 실행하더라도 서비스는 여전히 동일한 결과를 얻습니다. 테스트가 괜찮은지 어떻게 실제로 이해합니다.
응답 해 주셔서 감사합니다. 그러나 어떻게하면 내 방법이해야 할 일을하는지 테스트 할 수 있습니까? 예를 들어, 여기에서 필자의 메서드가 실제 매개 변수를 기반으로 데이터 검색을 수행하는지 테스트하고 싶습니다. – apoellitsi
당신은 환영합니다. 매개 변수를 기반으로 데이터를 테스트한다는 것은 단위 테스트의 범위를 벗어나는 백엔드 API 또는 계약을 테스트한다는 것을 의미합니다. –