角度测试中确定管道内部的测试值 [英] Test value inside finalize pipe in angular test
本文介绍了角度测试中确定管道内部的测试值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下情况:
app.component.ts
isLoading = false;
someFunction(){
this.isLoading = true;
this.someService
.someFuncInService
.pipe(finalize(() => (this.isLoading = false)))
.subscribe(data => {
...
});
}
现在,我想测试isLoading变量,无论它是否在末尾设置为false.
Now i want to test the isLoading variable, whether it is set false at the end or not.
我应该怎么做?Fixture.detectChanges()没有帮助
How should i do it? fixture.detectChanges() isn't helping
推荐答案
您需要执行以下操作:
在您的component.spec.ts中,添加一个模拟服务,并返回一个值,以便覆盖这行代码
In your component.spec.ts, add a mock service, and return a value so that this line of code gets covered
在component.spec.ts中,在describe()之前;写这个:
in component.spec.ts, before describe(); write this:
@Injectable()
class MockService extends SomeService {
someFunctionService() {
return of({your mock data})
}
}
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [yourComponent],
*************** ADD THIS LINE *************
providers: [
{
provide: SomeService,
useClass: MockService
}
]
}).compileComponents();
}));
// Your test case
it('should run #someFunction() method', () => {
component.isLoading = false;
spyOn(component, 'someFunction').and.callThrough();
component.someFunction();
expect(component.someFunction).toHaveBeenCalled();
})
这篇关于角度测试中确定管道内部的测试值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文