title: how do I mock out http post in Angular without using TestBed?
body: <p>How do I unit test this login function, specifically the http post part?  The http mock I made is not coded correctly to get into the 'if...else' section of the code.  I dont want to use TestBed.  TestBed is too slow.  </p>
  
  <pre><code>login(username: string, password: string): Observable&lt;boolean&gt; {
  
      const headers = new Headers();
      headers.append('Content-Type', 'application/json');
      headers.append('accept', 'application/json');
      let options = new RequestOptions({ headers: headers });
  
      return this.http.post('https://blah/api/login',
        JSON.stringify({ username: username, password: password }), options)
          .map((response: Response) =&gt; {
              const token = response.json() &amp;&amp; response.json().access_token;
              if (token) {
                  this.token = token;
                  localStorage.setItem('currentUser', JSON.stringify({ username: username, token: token }));
                  return true;
              } else {
                  return false;
              }
          }).catch(this._serverError);
  }
  
  private _serverError(err: any) {
      return Observable.throw(err || 'backend server error');
  }
  </code></pre>
  
  <hr>
  
  <p>Below is the Jasmine test Im attempting: I need help with this line. </p>
  
  <pre><code>spyOn(mockHttp,'post').and.returnValue(Observable.of(response));
  </code></pre>
  
  <p>What should my returnValue be to get my inside of the 'if...else' code in the login function?</p>
  
  <pre><code>    describe('AuthenticationService', () =&gt; {
        let service: AuthenticationService;
        let mockHttp = null;
  
        beforeEach(() =&gt; {
          mockHttp = {};
          mockHttp.post = function(){};
          service = new AuthenticationService(mockHttp);
       });
  
      it(`should set access token in local storage for successful login`,() =&gt; {
        const access_token = 'blah83balc380';
        const responseOptions =  new ResponseOptions();
        responseOptions.status = 200;
        responseOptions.body = {access_token:access_token};
        const username = 'test';
        const currentUserExpected = JSON.stringify({ username: username, token: access_token });
        var response = new Response(responseOptions);
        spyOn(mockHttp,'post').and.returnValue(Observable.of(response));
        service.login(username, 'test');
        var currentUser = localStorage.getItem('currentUser');
        expect(currentUserExpected).toEqual(currentUser);
      });
    });
  </code></pre>