如何编写测试用例以覆盖订阅回调函数中的行
How do I write test cases to cover the lines within subsribe callback function
在我的组件中,我有这段代码:
setTakenNcatsNames(): void {
if (this.settings.route){
this.dataService.getAll(this.settings.route).subscribe(result =>
{
console.log('in herer');
this.arrTakenNcatsNames = result
});
}
}
如何编写测试用例来覆盖 .subscribe() 回调中的行?无论我尝试什么,我在 .subscribe 中的行总是显示为未涵盖。[2]
我的报道总是显示这个:
`
setTakenNcatsNames(): void {
Eif (this.settings.route){
this.dataService.getAll(this.settings.route).subscribe(result => {
**console.log('in herer');**
**this.arrTakenNcatsNames = result**
});
}
}
`
这是我的测试用例,我在这里遗漏了什么?
describe('setTakenNcatsNames()', () => {
fit('sets the array for taken NCATS names', fakeAsync(() => {
component.setTakenNcatsNames();
}));
});
为 dataService.getAll
添加间谍
fit('sets the array for taken NCATS names', fakeAsync(() => {
const service: DataService = TestBed.get(DataService);
spyOn(service, "getAll").and.returnValue(of('test');// create a mock result instead of test
component.setTakenNcatsNames();
expect(component.arrTakenNcatsNames ).toBe('test');// or mock result
}));
这样你可以测试订阅下的区块
在我的组件中,我有这段代码:
setTakenNcatsNames(): void {
if (this.settings.route){
this.dataService.getAll(this.settings.route).subscribe(result =>
{
console.log('in herer');
this.arrTakenNcatsNames = result
});
}
}
如何编写测试用例来覆盖 .subscribe() 回调中的行?无论我尝试什么,我在 .subscribe 中的行总是显示为未涵盖。[2]
我的报道总是显示这个:
`
setTakenNcatsNames(): void {
Eif (this.settings.route){
this.dataService.getAll(this.settings.route).subscribe(result => {
**console.log('in herer');**
**this.arrTakenNcatsNames = result**
});
}
}
`
这是我的测试用例,我在这里遗漏了什么?
describe('setTakenNcatsNames()', () => {
fit('sets the array for taken NCATS names', fakeAsync(() => {
component.setTakenNcatsNames();
}));
});
为 dataService.getAll
fit('sets the array for taken NCATS names', fakeAsync(() => {
const service: DataService = TestBed.get(DataService);
spyOn(service, "getAll").and.returnValue(of('test');// create a mock result instead of test
component.setTakenNcatsNames();
expect(component.arrTakenNcatsNames ).toBe('test');// or mock result
}));
这样你可以测试订阅下的区块