title: test of angularjs controller: unsatistifed post request
body: <p>I'm testing an angularjs controller, using also mocks, but it raises the error 'Error: <code>Unsatisfied requests: POST /myurl</code></p>
  
  <p>My file for test contains a beforeEach method like this</p>
  
  <pre><code>httpBackend.whenPOST('/myurl')
       .respond( 200,obj1 );
       httpBackend.expectPOST('/myurl')
  
  
      scope = $rootScope.$new();
      MainCtrl = $controller('MyCtrl', {
          $scope:scope
      });
  </code></pre>
  
  <p>and my test case is:</p>
  
  <pre><code>it('scope.mymethod should work fine', function(){
  
          httpBackend.flush()
  
  
          // verify size of array before calling the method    
          expect(scope.myobjs.length).toEqual(2)
          // call the method
          scope.saveNewPage(myobj)
          // verify size of array after calling the method
          expect(scope.myobjs.length).toEqual(3)
  
      })
  </code></pre>
  
  <p>The method <code>saveNewPage</code> looks like:</p>
  
  <pre><code>function saveNewPage(p){
      console.log('Hello')
      $http.post('/myurl', {
                  e:p.e, url:p.url, name:p.name
              }).then(function (response) {
                      otherMethod(new Page(response.data.page))
                  }, handleError);
  }
  </code></pre>
  
  <p>Note that <code>console.log('Hello')</code> is never executed (in karma console it's never printed).</p>
  
  <p>EDIT:
  In the meanwhile I'm studying the doc about httpBackend, I tried to change the position of httpBackend.flush(). Basically, i'm executing a first flush(), to initialize data in the scope, then I execute the method, and then I execute an other flush() for the pending request. Specifically, in this situation the test case look like:</p>
  
  <pre><code>it('scope.saveNewPage  should work fine', function(){
          var p=new Object(pages[0])
  
          httpBackend.flush()
  
          httpBackend.whenPOST('/myurl',{
              url:pages[0].url,
              existingPage:new Object(pages[0]),
              name:pages[0].name
          }).respond(200,{data:pages[0]})
          httpBackend.expectPOST('/myurl')
  
          scope.saveNewPage(p)
  
          httpBackend.flush()
  
  
          expect(scope.pages.length).toBe(3)
  
  
      })
  </code></pre>
  
  <p>But now it raises <code>Error: No response defined !</code>, like if I didn't specified the mock for that url</p>