Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何模仿角形的土司特?_Angular_Unit Testing_Rxjs - Fatal编程技术网

Angular 如何模仿角形的土司特?

Angular 如何模仿角形的土司特?,angular,unit-testing,rxjs,Angular,Unit Testing,Rxjs,我不确定我是否写了一个关于这个问题的好标题。 如何模拟和绕过这2条红线 fit('Should call the sub entity list', () => { expect(component.subentity.length).toBe(0); spyOn(subentitysvc, 'getSubEntityList').and.returnValue(of(modelMock)); component.getSubEntityList(1);

我不确定我是否写了一个关于这个问题的好标题。 如何模拟和绕过这2条红线

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(() => { });

})

只要你使用jasmine,你就可以监视
toastr.error
方法:

spyOn(toastr, 'error').and.callThrough();

除了
callThrough
之外,您还可以使用
returnValue
callFake

我希望您在测试中没有正确设置toastr服务,因此请查看以下更改。您需要在创建组件之前侦察,使其也能工作

我把这行代码移到了spyOn(TestBed.get(ToastrService),'error')启动并假设您的烤面包机服务被称为
ToastrService

fit('应调用子实体列表',()=>{
expect(component.subentity.length).toBe(0);
spyOn(subentitysvc,'getSubEntityList')。和.returnValue(of(modelMock));
spyOn(TestBed.get(ToastrService),'error');
组件。getSubEntityList(1);
fixture.detectChanges();
expect(subentitysvc.getSubEntityList).toHaveBeenCalled();
})
希望这有帮助。
另外,我建议每个测试只有一个断言,因为这样更容易诊断问题。因此,您的第一行可能应该是
component.subentity=[]

实际上我已尝试调用fake,但问题是在调用fake之后,我不确定如何继续下一步:(此模拟的预期行为是什么。虽然我知道
error
方法返回
void
有什么问题?您有错误吗?请尝试将最后一个间谍移到
组件上方。getSubEntityList(1);
它会给我此错误消息。错误::找不到用于侦察错误的对象()用法:spyOn(,)组件中有
这个.toastr
,所以在测试中它应该是
组件。toastr
,不是吗?