我是Angular单元测试的新手。我正在测试服务A,但是服务A取决于服务B。
服务A
export class CmsService {
constructor(public httpService: HttpClientService) {}
getData(url): Observable<any> {
return this.httpService.get(url, { responseType: "text" }, "sonar");
}
getBannerData(url): Observable<any> {
return this.httpService.get(url, "", "cms");
}
}
public httpService: HttpClientService
这是我的 B服务
服务B
get(url, responeType, baseURL) {
return this.http
.get(this.endpoint + url, responeType)
.pipe(catchError(this.handleError));
}
服务规范
it("getUserList() should return data", () => {
service.getData("url").subscribe((res) => {
expect(res).toEqual(dummyUserDetails);
});
const req = httpMock.expectOne(
"https://serviceprovider.page"
);
expect(req.request.method).toBe("GET");
req.flush(dummyUserListResponse);
});
Error: Expected one matching request for criteria "Match URL: https://serviceprovider.page", found none. Requests received are: GET https://serviceprovider/json/url.
有人可以指导我对服务A做错了什么或如何对服务a进行测试。