我不确定是否为此问题写了一个好标题。 如何模拟并绕过这两条红线?
fit('Should call the sub entity list', () => {
expect(component.subentity.length).toBe(0);
spyOn(subentitysvc, 'getSubEntityList').and.returnValue(of(modelMock));
component.getSubEntityList(1);
fixture.detectChanges();
expect(subentitysvc.getSubEntityList).toHaveBeenCalled();
spyOn(toastr, 'error').and.callFake(() => { });
})
答案 0 :(得分:1)
只要您使用茉莉花,就可以监视---
tags:
- 1 - 2 -3
方法:
toastr.error
除了spyOn(toastr, 'error').and.callThrough();
之外,您还可以使用callThrough
或returnValue
。
答案 1 :(得分:0)
我希望您在测试中没有正确设置烤面包机服务,因此请查看以下内容以进行更改。 创建组件之前,您必须先进行间谍监视,以使其正常工作。
我已将这一行experiment('Healthcheck tests', () => {
describe('GET /v1/healthcheck', () => {
it('should fulfill healthcheck', async () => {
logger.log('there');
let options = {
method: 'GET',
url: '/v1/healthcheck',
};
let reviewsResponse = await serverInstance.api.hapi.inject(options);
expect(reviewsResponse.statusCode).to.equal(204);
});
});
});
向上移动,并假设您的烤面包机服务称为spyOn(TestBed.get(ToastrService), 'error');
。
ToastrService
希望这会有所帮助。
P.s.我建议每个测试仅包含一个断言,因为这样可以更轻松地诊断问题。因此,您的第一行可能应该是fit('Should call the sub entity list', () => {
expect(component.subentity.length).toBe(0);
spyOn(subentitysvc, 'getSubEntityList').and.returnValue(of(modelMock));
spyOn(TestBed.get(ToastrService), 'error');
component.getSubEntityList(1);
fixture.detectChanges();
expect(subentitysvc.getSubEntityList).toHaveBeenCalled();
})
。