title: Testing a database response in an Angularjs/Jasmine test
body: <p>I'm attempting to unit test an angular factory in jasmine but I'm having trouble understanding how to get an actual response from my database. </p>
  
  <p>I have the following factory code that retrieves an object containing company information based on a company ticker value. </p>
  
  <p>The factory works fine but I'd like to test it in jasmine.</p>
  
  <pre><code>    app.factory('adminOps',function($http, $log, $q){
        return {
          getByTicker: function(ticker){
  
              return $http.post("http://localhost:3000/ticker", {"ticker": ticker})
              .then(function(response){
                  if (typeof response.data === 'object') {
                      return response.data;
                  } else {
                      return $q.reject(response.data);
                  }
  
              }, function(response) {
                  return $q.reject(response);
              });
          }
        };
      });
  </code></pre>
  
  <p>To test this I have the following jasmine code based on online examples I found.</p>
  
  <pre><code>describe('adminOps', function() {
    var factory, http;
  
    beforeEach(module('myApp'));
    beforeEach(inject(function(_adminOps_, $httpBackend) {
      factory = _adminOps_;
      http = $httpBackend;
    }));
  
    it('Should retrieve company data', function(done) {
      var testCompany = function(company) {
        expect(company.name).toBe("Google Inc.");
      };
  
      var failTest = function(error) {
        expect(error).toBeUndefined();
      }
  
      http.expectPOST('http://localhost:3000/ticker',{ticker:"GOOG"}).respond(200, {ticker:"GOOG"});
  
      factory.getByTicker("GOOG")
        .then(testCompany)
        .catch(failTest)
        .finally(done);
  
      http.flush();
    });
  });
  </code></pre>
  
  <p>I get a <code>Expected undefined to be 'Google Inc.'</code>.</p>
  
  <p>How can I call my factory and test for the correct name value for the ticker parameter I send? </p>
  
  <p>UPDATE: Koen's code works correctly. I've found if you want to test values on a server, like a rest api, then you should try something like <a href="https://github.com/vlucas/frisby" rel="nofollow">frisby</a>, which is built on Jasmine.</p>