我正在为我的一个 Angular 组件方法编写单元测试,该方法为一个属性分配来自服务调用的响应值并调用另一个方法。
我的服务带有响应数据,我在测试中使用订阅中的期望语句订阅它,但它一直将属性的值显示为空数组。我已经确认下面测试中的“响应”包含模拟数据,但无法让组件属性“resultSet”显示为分配的值。“toggleSearchForm()”方法的间谍似乎也从未被调用过。
被测试的方法: search.component.ts
submitSearchCriteria() {
this.searchService.searchRequest(this.myForm.value)
.pipe(take(1))
.subscribe(response => {
this.resultSet = response;
this.toggleSearchForm();
});
}
失败的测试: search.component.spec.ts
it('should assign resultSet to response data and trigger toggle', fakeAsync(() => {
const spy = spyOn(component, 'toggleSearchForm');
component.myForm.controls['field1'].setValue('some search query');
component.myForm.controls['field2'].setValue('something that narrows it down more');
searchServiceStub.searchRequest(component.myForm.value)
.subscribe(response => {
expect(component.resultSet).toContain(response);
expect(spy).toHaveBeenCalled();
expect(spy.calls.count()).toBe(1);
});
tick();
}))
服务存根: search-service.stub.ts
...
const searchResults = require('./test-data/search-results.json');
searchRequest(searchCriteria) {
if (!searchCriteria) {
return of([])
}
return of(searchResults);
}
我希望 resultSet 包含存根响应和已调用的间谍,但测试失败并显示以下错误消息:
Expected [ ] to contain [ Object({ thingName: 'thing i was searching for', thingId: 1234 }) ].
和
Error: Expected spy toggleSearchForm to have been called.