Testing error case with observables in services
Let's say I have a component that subscribes to a service function: export class Component { ... ngOnInit() { this.service.doStuff().subscribe( (data: IData) => { doThings(data); }, (error: Error) => console.error(error) ); }; }; The subscribe call takes two anonymous functions as parameters, I've managed to set up a working unit test for the data function but Karma won't accept coverage for the error one. I've tried spying on the console.error function, throwing an error and then expecting the spy to have been called but that doesn't quite do it. My unit test: spyOn(console,'error').and