title: Testing error case with observables in services
body: <p>Let's say I have a component that subscribes to a service function:    </p>
  
  <pre><code>export class Component {
  
     ...
  
      ngOnInit() {
          this.service.doStuff().subscribe(
              (data: IData) =&gt; {
                doThings(data);
              },
              (error: Error) =&gt; console.error(error)
          );
      };
  };
  </code></pre>
  
  <p>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. </p>
  
  <p><a href="https://i.stack.imgur.com/buJ4F.png" rel="noreferrer"><img src="https://i.stack.imgur.com/buJ4F.png" alt="enter image description here"></a></p>
  
  <p>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.</p>
  
  <p>My unit test:</p>
  
  <pre><code>spyOn(console,'error').and.callThrough();
  
  serviceStub = {
          doStuff: jasmine.createSpy('doStuff').and.returnValue(Observable.of(data)),
      };
  
      serviceStub.doStuff.and.returnValue(Observable.throw(
  
          'error!'
      ));
  
  serviceStub.doStuff().subscribe(
  
      (res) =&gt; {
  
          *working test, can access res*
      },
      (error) =&gt; {
  
        console.error(error);
        console.log(error);  //Prints 'error!' so throw works.
        expect(console.error).toHaveBeenCalledWith('error!'); //Is true but won't be accepted for coverage.
      }
  );
  </code></pre>
  
  <p>What's the best practice for testing anonymous functions such as these? What's the bare minimum to secure test coverage?</p>