title: Get an Error "Expected spy <functionName> to have been called" in angular 6 jasmine
body: <p>I have a method "onEdit" in a component. In that method I've called service method "getAttributeById", I am doing a api call in "getAttributeById" service method and return an observable and I am writing a unit test case for it but I ma getting an error </p>
  
  <blockquote>
    <p>" Expected spy getAttributeById to have been called."</p>
  </blockquote>
  
  <pre><code>onEdit(row) {
      if (this.role != 'Admin' &amp;&amp; this.role != 'Owner') {
        return;
      }
      this.loadingData = true;
      this._service.getAttributeById(row['id']).subscribe((data) =&gt; {
        this.editing = true;
        this.newAttribute = { ...data };
        data.isEdit = true;
  
        this.loadingData = false;
        this._service.setData({attribute: this.newAttribute});
        this.router.navigate(['attributes-edit']);
      })   }
  </code></pre>
  
  <p>spec file</p>
  
  <pre><code>describe('MetaDataComponent', () =&gt; {
    let component: MetaDataComponent;
    let fixture: ComponentFixture&lt;MetaDataComponent&gt;;
    beforeEach(() =&gt; {
      const metaDataServiceStub = {
        getVariables: query1 =&gt; ({ subscribe: () =&gt; ({}) }),
        getAttributeById: arg1 =&gt; ({ subscribe: () =&gt; ({}) }),
        setData: object1 =&gt; ({}),
        deleteAttribute: arg1 =&gt; ({ subscribe: () =&gt; ({}) })
      };
      const routerStub = { navigate: array1 =&gt; ({}) };
      TestBed.configureTestingModule({
        schemas: [NO_ERRORS_SCHEMA],
        declarations: [MetaDataComponent],
        providers: [
          { provide: MetaDataService, useValue: metaDataServiceStub },
          { provide: Router, useValue: routerStub }
        ]
      });
      spyOn(MetaDataComponent.prototype, 'getAttributes');
      fixture = TestBed.createComponent(MetaDataComponent);
      component = fixture.componentInstance;
    });
  it('onEdit', () =&gt; {
        const payload = {id: '11111'};
        const metaDataServiceStub: MetaDataService = fixture.debugElement.injector.get(MetaDataService);
        const editSpy = spyOn(metaDataServiceStub, 'getAttributeById').and.returnValue(of({}));
        expect(component.onEdit(payload)).toBeUndefined();
        expect(editSpy).toHaveBeenCalled();
      });
  </code></pre>
  
  <p>I am getting below error</p>
  
  <pre><code>Expected spy getAttributeById to have been called.
  Error: Expected spy getAttributeById to have been called.
      at stack (http://localhost:9876/absoluteC:/Users/U1192079/Projects/r2d2-ui/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2455:17)
      at buildExpectationResult (http://localhost:9876/absoluteC:/Users/U1192079/Projects/r2d2-ui/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2425:14)
      at Spec.expectationResultFactory (http://localhost:9876/absoluteC:/Users/U1192079/Projects/r2d2-ui/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:901:18)
      at Spec.addExpectationResult (http://localhost:9876/absoluteC:/Users/U1192079/Projects/r2d2-ui/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:524:34)
      at Ex
  </code></pre>