因此,当我查看报道报告时,我会通过karma-coverage-instanbul-reporter跟踪单元测试覆盖率:
.map(res => res.json())
我所有提供商的一部分从未在测试中受到影响。但我确实对这些提供商进行了测试,并且正在验证我是否收到了回复。
以下是我正在测试的函数的示例:
public getCampaigns(): Observable<CampaignsResponse> {
return this.http.get(this.campaignsUrl).map(res => res.json());
}
以下是此功能的测试:
describe("getCampaigns", () => {
it("should return an Observable<CampaignsResponse>",
inject([CampaignsService, MockBackend], (service: CampaignsService, mockBackend: MockBackend) => {
// arrange
mockBackend.connections.subscribe((connection: any) => {
connection.mockBackend(new Response(new ResponseOptions({
body: JSON.stringify(mockCampaigns),
})));
});
// act
service.getCampaigns().subscribe((campaigns) => {
// assert
expect(campaigns.campaigns.length).toEqual(1);
expect(campaigns.campaigns[0].id).toEqual(mockCampaigns.campaigns[0].id);
expect(campaigns.campaigns[0].channelID).toEqual(mockCampaigns.campaigns[0].channelID);
expect(campaigns.campaigns[0].name).toEqual(mockCampaigns.campaigns[0].name);
expect(campaigns.campaigns[0].active).toEqual(mockCampaigns.campaigns[0].active);
expect(campaigns.campaigns[0].createdAt).toEqual(mockCampaigns.campaigns[0].createdAt);
expect(campaigns.campaigns[0].updatedAt).toEqual(mockCampaigns.campaigns[0].updatedAt);
});
}));
});
但是根据我的测试覆盖率,res.json()永远不会在地图功能中被击中。
有没有人对这可能是什么有任何想法? Coverage报告可能是错误的还是将代码错误地映射回TypeScript?