title: TypeError: Cannot read property 'returnValue' of undefined
body: <pre><code>import { async, ComponentFixture, TestBed } from '@angular/core/testing';
  
  import { ProductListComponent } from './product-list.component';
  import { FormsModule } from '@angular/forms';
  import { DebugElement } from '@angular/core';
  import { By } from '@angular/platform-browser';
  import { ProductService } from './product.service';
  
  describe('ProductListComponent', () =&gt; {
    let component: ProductListComponent;
    let fixture: ComponentFixture&lt;ProductListComponent&gt;;
    let debugElement: DebugElement;
    let productService: ProductService;
  
    beforeEach(async(() =&gt; {
      TestBed.configureTestingModule({
        declarations: [ ProductListComponent ],
        imports: [FormsModule],
        providers: [ProductService]
      })
      .compileComponents();
    }));
  
    beforeEach(() =&gt; {
      fixture = TestBed.createComponent(ProductListComponent);
      productService = TestBed.get(ProductService);
      component = fixture.componentInstance;
      fixture.detectChanges();
      debugElement = fixture.debugElement;
    });
  
  
  
    fit('should test filter product list (done)', (done)=&gt; {
      //component.searchText='fresh';
      let productSpy = spyOn(productService, 'filterProductList').withArgs('fresh').and.callThrough();
  
  
      productSpy.calls.mostRecent().returnValue.then(()=&gt;{
        fixture.detectChanges();
        const value = debugElement.query(By.css('#product_0')).nativeElement.innerText;
        expect(value).toContain(component.searchText);
        done();
      });
  
  
    });
  
  });
  </code></pre>
  
  <p>I am trying to write the test case to filter the product list and have written a product spy, but it is giving an error: </p>
  
  <pre><code>TypeError: Cannot read property 'returnValue' of undefined
  </code></pre>
  
  <p>for the following line:</p>
  
  <pre><code>productSpy.calls.mostRecent().returnValue.then
  </code></pre>
  
  <p>I tried to find the solution online, but could not find a suitable solution.</p>
  
  <p>Following is the service I am trying test:
  1. filterProductList - will take in a string and filter through the list of product names and get the list of products - one matching the given string parameter</p>
  
  <pre><code>@Injectable({
    providedIn: 'root'
  })
  export class ProductService {
    productList = PRODUCT_LIST;
  
    constructor() { }
    public getProductList(){
      return of(this.productList);
    }
  
    public filterProductList(searchString: string): Promise&lt;any&gt;{
      let dataObs: Observable&lt;any&gt;;
      //dataObs = of(this.productList.filter( product =&gt; product.title.toLowerCase().indexOf(searchString) &gt; -1));
      //setTimeout(()=&gt; { dataObs = of(this.productList.filter( product =&gt; product.title.toLowerCase().indexOf(searchString) &gt; -1))},1000);
      return of(this.productList.filter( product =&gt; product.title.toLowerCase().indexOf(searchString) &gt; -1)).toPromise();
    }
  }
  
  export const PRODUCT_LIST = [{
    "title": "Brown eggs",
    "type": "dairy",
    "description": "Raw organic brown eggs in a basket",
    "filename": "0.jpg",
    "height": 600,
    "width": 400,
    "price": 28.1,
    "rating": 4
  }];
  </code></pre>