我有一个方法在服务类,它有一个对后端服务器的 GET 调用,后端服务器返回一个带有字段的人数组的响应 - 就业 ID、姓名和 busnTitle。
你能帮助覆盖以下几行吗?
searchPeople(searchWord: string): Observable<People[]> {
return this.http.get<any>("{URL}").pipe(
map(data => {
if (data) {
return data.persons.map(p => {
return {
eId: p.emplNtId,
name: p.name,
jobTitle: p.busnTitle
};
});
}
})
);
}
Solution:
1) people-list-mock.json
{
"persons": [{
"emplNtId:": "123456",
"name": "testname",
"busnTitle": "Tester"
},
{
"emplNtId:": "1234",
"name": "testname",
"busnTitle": "Developer"
}
]
}
Added this in ts file
const peopleMockResponse: any = require('people-list-mock.json');
it('should return an Observable<People[]> - success', () => {
peopleService.searchPeople(mockName).subscribe(
(people) => {
expect(people.length).toBe(2);
});
const req = httpTestingController.expectOne('{URL}');
expect(req.request.method).toBe('GET');
req.flush(peopleMockResponse);
httpTestingController.verify();
});
it('should not return response - failed ', () => {
peopleService.searchPeople(mockName).subscribe(
(people) => {
expect(people).toBeUndefined();
});
const req = httpTestingController.expectOne('{URL}');
expect(req.request.method).toBe('GET');
req.flush(null);
httpTestingController.verify();
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句