title: Cannot Read Property 'subscribe' of Angular Test Mock
body: <p>I'm trying to unit test an Angular component that has an injected service. In the component's constructor, a method on the injected service is called that returns an Observable. I'm trying to mock the service in my component's unit test, but I keep running into this error: <code>TypeError: Cannot read property 'subscribe' of undefined</code>.</p>
  
  <p>I've tried to mock the service in the following ways:</p>
  
  <pre><code>const serviceStub = {
    getObservable: () =&gt; { return {subscribe: () =&gt; {}}; },
  };
  
  beforeEach(async(() =&gt; {
    TestBed.configureTestingModule({
      providers: [
        {provide: MyService, useValue: serviceStub}
      ]
  })
  
  
  it('should create', () =&gt; {
    spyOn(serviceStub, 'getObservable').and.returnValue({subscribe: () =&gt; {}});
    expect(component).toBeTruthy();
  });
  </code></pre>
  
  <p>It feels like I'm missing something obvious. Can someone please point it out?</p>
  
  <h2>UPDATE</h2>
  
  <p>I get this error even when I inject the actual service in my test bed providers.</p>
  
  <p>The component's constructor looks something like this:</p>
  
  <pre><code>private _subscription: Subscription;
  
  constructor(private _service: MyService) {
    this._subscription = _service.getObservable().subscribe(console.log);
  }
  </code></pre>