title: Angular Js - Test POST request that returns an object with Jasmine
body: <p>I have a service set up which makes all my AJAX calls. I want to test my login method, which sends an AJAX POST <code>$http.post</code> to a particular URL, which returns an objects with the result (login passed or failed). This result is an object. I have not return the code exactly to test the service, but I'm trying to test the URL first. This is how it looks right now:</p>
  
  <pre><code>'use strict';
  
  describe('Service: Ajax', function () {
  
      var service, httpBackend;
  
      // load the service's module
      beforeEach(module('mySampleApp'));
  
      // instantiate service
      beforeEach(inject(function (Ajax, _$httpBackend_) {
          httpBackend = _$httpBackend_;
          service = Ajax;
      }));
  
      it('Test AJAX call', function () {
          httpBackend.expect('POST', 'http://myloginurl', {u: 'xyz', p: 'pass'}, { withCredentials: true})
          .respond(201, 'success');
      });
  });
  </code></pre>
  
  <p>This passes. Now I tried putting a wrong URL, wrong username/password, but it still passes! How do I test this?</p>
  
  <p>UPDATE:
  Better written now:</p>
  
  <pre><code>//Ajax is my AJAX service
  it('should test the login AJAX call', inject(function (Ajax) {
      httpBackend.expect('POST', 'http://myloginurl')
          .respond(200, "[{ status: 200, //some more data }]");
      httpBackend.flush();
      Ajax.authenticate({u: 'xyz', password: 'pass' })
          .then(function(data){
              expect(data.status).toBe(200);
          });
  }));
  </code></pre>
  
  <p>I get this:</p>
  
  <pre><code>PhantomJS 1.9.7 (Linux) Service: Ajax should test the login AJAX call FAILED
  Error: No pending request to flush !
  blah blah...
  </code></pre>