title: Parameter of callback is undefined in service call. Jasmine unit test failing (Angular 2)
body: <p>I've been stuck on this one for 2 days now.  I have a unit test for a service call that's failing.   Here's the service code [nummeasurements.service.ts]:</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { Http, Response, Headers, RequestOptions } from '@angular/http';
  import {Observable} from 'rxjs/Rx';
  import 'rxjs/add/operator/map'
  import {ServiceConfiguration} from '../../../serviceconfiguration';
  import {MeasurementCount} from './measurementcount.model';
  
  
  @Injectable()
  export class NumMeasurementsService {
  
    private configuration: ServiceConfiguration;
    private numMeasurementsUrl: string;
    private measurementCount: MeasurementCount;
  
  
    constructor (private http: Http, configuration: ServiceConfiguration) {
      // console.log(configuration);
  
      this.numMeasurementsUrl = configuration.baseURL + '/getnumberofmeasurements';
      this.configuration = configuration;
      // console.log("this.numProbesUrl: " + this.numProbesUrl);
    }
  
    createAuthorizationHeader(headers: Headers) {
  
      let user = this.configuration.user;
      let apisecret = this.configuration.apiSecret;
  
      headers.append('X-Auth-Key', user);
      headers.append('X-Auth-Secret', apisecret);
    }
  
  
    public getMeasurementCount() : Observable&lt;MeasurementCount&gt; {
      let headers = new Headers();
      this.createAuthorizationHeader(headers);
  
      return this.http.get(this.numMeasurementsUrl, {headers: headers})
        .map((response:Response) =&gt; {
            this.measurementCount = &lt;MeasurementCount&gt;response.json();
            // console.log(response.json());
            }
        )
        .catch((error:any) =&gt; Observable.throw(error.json().error || 'Server error'));
    }
  
  }
  </code></pre>
  
  <p>Here's [serviceconfiguration.ts]:</p>
  
  <pre><code>import {Injectable} from '@angular/core';
  
  @Injectable()
  
  export class ServiceConfiguration {
  
    baseURL: string;
    user: string;
    apiSecret: string;
  
    constructor() {
      this.baseURL = 'http://localhost:9090';
      this.user = 'mjones010c';
      this.apiSecret = '123e4567-e89b-12d3-a456-426655440000';
    }
  
  }
  </code></pre>
  
  <p>Here's the actual spec that's throwing the error [nummeasurements.service.spec.ts]:</p>
  
  <pre><code>import {
    TestBed,
    getTestBed,
    async,
    inject
  } from '@angular/core/testing';
  import {
    Headers, BaseRequestOptions,
    Response, HttpModule, Http, XHRBackend, RequestMethod
  } from '@angular/http';
  
  import {ResponseOptions} from '@angular/http';
  import {MockBackend, MockConnection} from '@angular/http/testing';
  
  import {NumMeasurementsService} from './nummeasurements.service';
  import {MeasurementCount} from "./measurementcount.model";
  import {ServiceConfiguration} from "../../../serviceconfiguration";
  
  describe('NumMeasurementsService', () =&gt; {
    let mockBackend: MockBackend;
  
    let serviceConfiguration = new ServiceConfiguration();
  
    beforeEach(async(() =&gt; {
      TestBed.configureTestingModule({
        providers: [
          NumMeasurementsService,
          MockBackend,
          BaseRequestOptions,
          ServiceConfiguration,
          {
            provide: Http,
            deps: [MockBackend, BaseRequestOptions],
            useFactory: (backend: XHRBackend, defaultOptions: BaseRequestOptions) =&gt; {
              return new Http(backend, defaultOptions);
            }
          }
        ],
        imports: [
          HttpModule
        ]
      });
      mockBackend = getTestBed().get(MockBackend);
    }));
  
  
    it('should be defined', () =&gt; {
      let numMeasurementsService: NumMeasurementsService;
      getTestBed().compileComponents();
  
      numMeasurementsService = getTestBed().get(NumMeasurementsService);
      expect(numMeasurementsService).toBeDefined();
  
    })
  
    it('should be defined', () =&gt; {
      let numMeasurementsService: NumMeasurementsService;
      getTestBed().compileComponents();
  
      numMeasurementsService = getTestBed().get(NumMeasurementsService);
      expect(numMeasurementsService).toBeDefined();
  
    })
  
  
    it('should get measurement count', done =&gt; {
      let numMeasurementsService: NumMeasurementsService;
    //
      getTestBed().compileComponents().then(() =&gt; {
        mockBackend.connections.subscribe(
          (connection: MockConnection) =&gt; {
            connection.mockRespond(new Response(
              new ResponseOptions({
                  body: {
                    "totalMeasurementCount": 17,
                    "measurementInfo": [
                      {
                        "measurementType": "DNS",
                        "measurementCount": 6
                      },
                      {
                        "measurementType": "Ping",
                        "measurementCount": 11
                      }
                    ]
                  }
                }
              )));
          });
  
        numMeasurementsService = getTestBed().get(NumMeasurementsService);
        expect(numMeasurementsService).toBeDefined();
  
        numMeasurementsService.getMeasurementCount().subscribe((measurementCount: MeasurementCount) =&gt; {
  
          expect(measurementCount).toBeDefined();
          // expect(measurementCount.totalMeasurementCount).toEqual(17);
  
          done();
        });
      });
    });
  }
  </code></pre>
  
  <p>The test is failing here at expect(measurementCount).toBeDefined();:</p>
  
  <pre><code>numMeasurementsService.getMeasurementCount().subscribe((measurementCount: MeasurementCount) =&gt; {
  
      expect(measurementCount).toBeDefined();
      // expect(measurementCount.totalMeasurementCount).toEqual(17);
  
      done();
    });
  </code></pre>
  
  <p>And here's the mock service that it is using: </p>
  
  <pre><code>import { Observable } from 'rxjs/Observable';
  import { MeasurementCount } from './measurementcount.model';
  
  export class NumMeasurementsMockService {
  
    measurementCount : MeasurementCount; // = new MeasurementCount(3);
  
    public getMeasurementCount(): Observable&lt;MeasurementCount&gt; {
      return Observable.of(this.measurementCount);
    }
  
  }
  </code></pre>
  
  <p>Here's the actual error output that I'm getting from Karma:</p>
  
  <pre><code>Chrome 56.0.2924 (Mac OS X 10.10.5) NumMeasurementsService should get measurement count FAILED
      Expected undefined to be defined.
          at SafeSubscriber._next (webpack:///src/app/dashboard/infobox/nummeasurements/nummeasurements.service.spec.ts:98:33 &lt;- src/test.ts:90347:42) [ProxyZone]
          at SafeSubscriber.__tryOrSetError (webpack:///~/rxjs/Subscriber.js:232:0 &lt;- src/test.ts:644:16) [ProxyZone]
          at SafeSubscriber.next (webpack:///~/rxjs/Subscriber.js:174:0 &lt;- src/test.ts:586:27) [ProxyZone]
          at Subscriber._next (webpack:///~/rxjs/Subscriber.js:125:0 &lt;- src/test.ts:537:26) [ProxyZone]
          at Subscriber.next (webpack:///~/rxjs/Subscriber.js:89:0 &lt;- src/test.ts:501:18) [ProxyZone]
          at CatchSubscriber.Subscriber._next (webpack:///~/rxjs/Subscriber.js:125:0 &lt;- src/test.ts:537:26) [ProxyZone]
          at CatchSubscriber.Subscriber.next (webpack:///~/rxjs/Subscriber.js:89:0 &lt;- src/test.ts:501:18) [ProxyZone]
          at MapSubscriber._next (webpack:///~/rxjs/operator/map.js:83:0 &lt;- src/test.ts:13871:26) [ProxyZone]
  Chrome 56.0.2924 (Mac OS X 10.10.5): Executed 9 of 9 (1 FAILED) (0.678 secs / 0.493 secs)
  </code></pre>
  
  <p>Any ideas?  What's crazy is that I've copy and pasted the code from another service that seems to be working just fine.  I've diffed the files and been over them a million times but can't make out why this value keeps coming up undefined.  Any help is greatly appreciated.</p>