title: How to preserve this when calling a function in Jasmine
body: <p>I am writing tests for a service.</p>
  
  <p>The snippet of the service I am testing is:</p>
  
  <pre class="lang-js prettyprint-override"><code> getDesign(designId: number): Observable&lt;any&gt; {
      const params = new HttpParams().set('designId', String(designId));
      return this.http
          .get('/get-design', { params: params })
          .pipe(retry(1), map((res) =&gt; res['data'] || []), catchError(this.handleError));
  }
  </code></pre>
  
  <p>The snippet of spec file is:</p>
  
  <pre class="lang-js prettyprint-override"><code> fit('getDesign should append params and return', () =&gt; {
      const response = {
          'success': true,
          'data': [{foo: 'bar'}],
          'metadata': {'version': '1.8.25', 'versionHash': '14e456e062583f176d25'},
          'total': 0,
          'message': []
      };
      // service.getDesign(42).subscribe((res) =&gt; {
      //     expect(res).toEqual([{foo: 'bar'}]);
      // });
      //
      // const call: TestRequest = httpMock.expectOne('/get-design?designId=42');
      // expect(call.request.method).toEqual('GET');
      // call.flush(response);
  
      doServiceTest(service.getDesign, response, 42, [{foo: 'bar'}],
          '/get-design?designId=42', 'POST');
  });
  
  function doServiceTest(myFunction: Function, myResponse, myParam, myExpected, myUrl, myMethod) {
      myFunction.bind(this);
      myFunction(myParam).subscribe((res) =&gt; {
         expect(res).toEqual(myExpected);
      });
      const call: TestRequest = httpMock.expectOne(myUrl);
      expect(call.request.method).toEqual(myMethod);
      call.flush(myResponse);
  }
  </code></pre>
  
  <p>The code which is commented out works just fine, so it is not an issue with something not being injected or otherwise not referenced, which invalidates the answers to other Stack Overflow posts and search results I have run across. </p>
  
  <p>I would prefer to use the other approach as I have multiple functions which vary only by <code>params</code> and <code>expected</code>. </p>
  
  <p>But when I try this way I get an error of <code>TypeError: Cannot read property 'http' of undefined</code> which says to me that <code>this</code> reference is somehow lost. </p>
  
  <p>What am I doing wrong?</p>
  
  <p>I even tried explicitly binding both service and this, but it didn't solve the problem.</p>