title: Testing backend API via $http in AngularJS/karma/jasmine tests?
body: <p>How do I test my API backend using AngularJS/karma/jasmine tests?</p>
  
  <p>I have tried to create the smallest test-case showing my error:</p>
  
  <h3>echo_server.py</h3>
  
  <pre class="lang-py prettyprint-override"><code>from bottle import response, route, run
  
  @route('/echo/&lt;echo&gt;')
  def echo_echo(echo):
      response.headers['Access-Control-Allow-Origin'] = '*'
      return {'echo': echo}    # Served as JSON
  
  if __name__ == '__main__':
      run()
  </code></pre>
  
  <h3>test/unit/apiSpec.js</h3>
  
  <pre><code>// Should this be in `test/e2e/apiSpec.js`?
  describe('echo', function () {
      var scope, http;
  
      beforeEach(inject(function ($rootScope, $http) {
          $http.defaults.useXDomain = true;    // CORS
          scope = $rootScope.$new();
          http = $http;
      }));
  
  
      it("should echo from server", function () {
          scope.$apply(function () {
              var ret;
              http.get("http://localhost:8080/echo/foo")
                  .success(function (data, status, headers, config) {
                               ret = data;
                           })
                  .error(function (data, status, headers, config) {
                             ret = data;
                         });
              console.log('ret = ' + ret);
              expect(ret.echo).toBe("foo");
          });
      });
  });
  </code></pre>
  
  <h3>Output of <code>karma start configs/karma.conf.js</code></h3>
  
  <pre class="lang-none prettyprint-override"><code>INFO [karma]: Karma v0.10.8 server started at http://localhost:9876/
  INFO [launcher]: Starting browser PhantomJS
  INFO [PhantomJS 1.9.2 (Linux)]: Connected on socket m7imfgmhXdOY9JVKJh8r
  LOG: 'ret = undefined'
  PhantomJS 1.9.2 (Linux) echo should echo from server FAILED
      Error: Unexpected request: GET http://localhost:8080/echo/foo
      No more request expected
          at $httpBackend (~Projects/~karma-tests/test/lib/angular/angular-mocks.js:1178)
      ...
  </code></pre>