Reputation: 1127
While running code coverage tests I noticed that I didn't cover error handling on subscriber functions. Function to test:
getVersion() {
return this.aboutService.getAPIVersion()
.subscribe(
info => {
console.log('info', info);
},
error => {
console.log('error', error);
}
);
}
This is function is in the component and it's calling function from service. I managed to write unit test for function from service with mockBackend abd MockError but I don;t know how to do that with wrapper(caller) function.
So far I mocked service with class but I am only covering response and not the errors:
class AboutServiceStub {
getAPIVersion = jasmine.createSpy('getAPIVersion').and.callFake(
this.fakedGetAPIVersion
);
fakedGetAPIVersion() {
console.log('fakedGetAPIVersion');
return Observable.of(new Object(version))
.map(version => JSON.parse(JSON.stringify(version)));
}
}
Upvotes: 1
Views: 1523
Reputation: 1103
I had the same scenario where i needed to test the error case just for one test case(for the sake of code coverage).The approach i followed is 1.Get the injected service into the test as follows
aboutService = fixture.debugElement.injector.get(AboutService);
2.Now override the method using
aboutService.getAPIVersion = () => Observable.throw('error');
SO for this particular test the method has been overridden. The exact code might not work,This is something i remember on top of my head .
Upvotes: 1