Angular 下标后角5单元测试中的测试误差
我的Angular 5控制器中有以下函数,我想在其上使用karma进行单元测试Angular 下标后角5单元测试中的测试误差,angular,unit-testing,typescript,karma-jasmine,karma-coverage,Angular,Unit Testing,Typescript,Karma Jasmine,Karma Coverage,我的Angular 5控制器中有以下函数,我想在其上使用karma进行单元测试 this.search.getFirstSearch().subscribe(data => { this.processSchool = data.search3, error =><any> this.errorMessage, ()=>{ if(this.errorMessage ===''){ console
this.search.getFirstSearch().subscribe(data => {
this.processSchool = data.search3,
error =><any> this.errorMessage,
()=>{
if(this.errorMessage ===''){
console.log('abc')
}else{
console.log('xxxx')
}
}
});
从生成的报告中,不包括错误部分。我错过了什么
您可以尝试使用.and.throwerr,如前所述和
根据您所拥有的,我认为您可能有一些无关的花括号来包装传递给subscribe方法的参数。订阅([onNext]、[onError]、[onCompleted])。否则,您已经设置了不分配任何变量且永远不会运行的方法(这就是为什么它说代码永远不会运行的原因)
it('should simulate error 1 ', () => {
fixture.detectChanges();
const searchService = fixture.debugElement.injector.get(SearchService);
const mockCall= spyOn(searchService, 'getFirstSearch').and.returnValue(Observable.throw({status: 404}));
//call method
comp.processData();
});
it('should simulate error 2 ', () => {
const searchService = fixture.debugElement.injector.get(SearchService);
const mockCall= spyOn(searchService, 'getFirstSearch').and.returnValue(Observable.throw({message: ""}));
//call method
comp.processData();
});
it('should simulate error 3 ', async(() => { fixture.detectChanges();
const searchService = fixture.debugElement.injector.get(SearchService);
let error404 = {status: 404};
backend.connections.subscribe((connection: MockConnection) => {
connection.mockError(error404 as any as Error);
});
comp.processData();
}));
////added test
it('should simulate error 4 ', () => { fixture.detectChanges();
const searchService = fixture.debugElement.injector.get(SearchService);
const mockCall= spyOn(searchService, 'getFirstSearch').and.returnValue(
new ErrorObservable('TwainService test failure'))
comp.processData();
});
it('should simulate error 5', () => { fixture.detectChanges();
const searchService = fixture.debugElement.injector.get(SearchService);
const mockCall= spyOn(searchService, 'getFirstSearch').and.returnValue(Observable.throw({message: ""}));
comp.processData();
});
spyOn(searchService, 'getFirstSearch').and.throwError({status: 404});
this.search.getFirstSearch().subscribe(
data => this.processSchool = data.search3, // onNext
error => this.errorMessage = error, // onError
() => { // onCompleted
if(this.errorMessage === '') {
console.log('abc')
} else {
console.log('xxxx')
}
}
);