title: Testing AngularJS controllers using expectPOST
body: <p>I've got a AngularJS/Rails app and I want to test my AngularJS controller is posting to the backend server when creating a new record. (I'm using jasmine for my tests)</p>
  
  <p>Here is my attempted test</p>
  
  <pre><code>  describe "create", -&gt;
      beforeEach(inject ( ($controller, $rootScope, $location, $state, $httpBackend) -&gt;
        @redirect = spyOn($location, 'path')
        @httpBackend.whenGET('/assets/layouts/default.html.erb').respond(200)
        @httpBackend.whenGET('/assets/letters/index.html.erb').respond(200)
        @httpBackend.whenPOST('/api/letters').respond(200)
  
        $controller("LettersController", { $scope: @scope, $location: @location })
      ))
  
      it "sends a post to the backend", -&gt;
        @httpBackend.expectPOST('/api/letters', {"letter":{},"_utf8":"☃"}).respond(200)
        @scope.create()
  </code></pre>
  
  <p>Here s the code which I'm testing:</p>
  
  <pre><code>  $scope.create = -&gt;
      Letter.save(
        {}
      ,
        letter:
          subject: $scope.letter.subject
          body: $scope.letter.body
  
      # success
      , (response) -&gt;
        $location.path "/letters"
      # failure
      , (response) -&gt;
      )
  </code></pre>
  
  <p>The code in question works correctly and the test passes. The problem is if I comment my Letter.save code out (which makes the post through AngularJS resources) then my test still passes.</p>
  
  <p>How can I get my test to work properly?</p>
  
  <p>My full test application is here: <a href="https://github.com/map7/angularjs_rails_example2" rel="nofollow">https://github.com/map7/angularjs_rails_example2</a></p>