diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ngMock/angular-mocks.js | 137 | 
1 files changed, 81 insertions, 56 deletions
diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 7681449b..9364390a 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -832,75 +832,100 @@ angular.mock.dump = function(object) {   *   *   * # Unit testing with mock $httpBackend + * The following code shows how to setup and use the mock backend in unit testing a controller. + * First we create the controller under test   * - * <pre> -   // controller -   function MyController($scope, $http) { -     $http.get('/auth.py').success(function(data) { -       $scope.user = data; -     }); - -     this.saveMessage = function(message) { -       $scope.status = 'Saving...'; -       $http.post('/add-msg.py', message).success(function(response) { -         $scope.status = ''; -       }).error(function() { -         $scope.status = 'ERROR!'; -       }); -     }; -   } - -   // testing controller -   var $httpBackend; +  <pre> +  // The controller code +  function MyController($scope, $http) { +    var authToken; -   beforeEach(inject(function($injector) { -     $httpBackend = $injector.get('$httpBackend'); +    $http.get('/auth.py').success(function(data, status, headers) { +      authToken = headers('A-Token'); +      $scope.user = data; +    }); -     // backend definition common for all tests -     $httpBackend.when('GET', '/auth.py').respond({userId: 'userX'}, {'A-Token': 'xxx'}); -   })); +    $scope.saveMessage = function(message) { +      var headers = { 'Authorization': authToken }; +      $scope.status = 'Saving...'; +      $http.post('/add-msg.py', message, { headers: headers } ).success(function(response) { +        $scope.status = ''; +      }).error(function() { +        $scope.status = 'ERROR!'; +      }); +    }; +  } +  </pre> + * + * Now we setup the mock backend and create the test specs. + *  +  <pre> +    // testing controller +    describe('MyController', function() { +       var $httpBackend, $rootScope, createController; + +       beforeEach(inject(function($injector) { +         // Set up the mock http service responses +         $httpBackend = $injector.get('$httpBackend'); +         // backend definition common for all tests +         $httpBackend.when('GET', '/auth.py').respond({userId: 'userX'}, {'A-Token': 'xxx'}); + +         // Get hold of a scope (i.e. the root scope) +         $rootScope = $injector.get('$rootScope'); +         // The $controller service is used to create instances of controllers +         var $controller = $injector.get('$controller'); + +         createController = function() { +           return $controller('MyController', {'$scope' : $rootScope }); +         }; +       })); + + +       afterEach(function() { +         $httpBackend.verifyNoOutstandingExpectation(); +         $httpBackend.verifyNoOutstandingRequest(); +       }); -   afterEach(function() { -     $httpBackend.verifyNoOutstandingExpectation(); -     $httpBackend.verifyNoOutstandingRequest(); -   }); +       it('should fetch authentication token', function() { +         $httpBackend.expectGET('/auth.py'); +         var controller = createController(); +         $httpBackend.flush(); +       }); -   it('should fetch authentication token', function() { -     $httpBackend.expectGET('/auth.py'); -     var controller = scope.$new(MyController); -     $httpBackend.flush(); -   }); +       it('should send msg to server', function() { +         var controller = createController(); +         $httpBackend.flush(); -   it('should send msg to server', function() { -     // now you don’t care about the authentication, but -     // the controller will still send the request and -     // $httpBackend will respond without you having to -     // specify the expectation and response for this request -     $httpBackend.expectPOST('/add-msg.py', 'message content').respond(201, ''); +         // now you don’t care about the authentication, but +         // the controller will still send the request and +         // $httpBackend will respond without you having to +         // specify the expectation and response for this request -     var controller = scope.$new(MyController); -     $httpBackend.flush(); -     controller.saveMessage('message content'); -     expect(controller.status).toBe('Saving...'); -     $httpBackend.flush(); -     expect(controller.status).toBe(''); -   }); +         $httpBackend.expectPOST('/add-msg.py', 'message content').respond(201, ''); +         $rootScope.saveMessage('message content'); +         expect($rootScope.status).toBe('Saving...'); +         $httpBackend.flush(); +         expect($rootScope.status).toBe(''); +       }); -   it('should send auth header', function() { -     $httpBackend.expectPOST('/add-msg.py', undefined, function(headers) { -       // check if the header was send, if it wasn't the expectation won't -       // match the request and the test will fail -       return headers['Authorization'] == 'xxx'; -     }).respond(201, ''); +       it('should send auth header', function() { +         var controller = createController(); +         $httpBackend.flush(); +          +         $httpBackend.expectPOST('/add-msg.py', undefined, function(headers) { +           // check if the header was send, if it wasn't the expectation won't +           // match the request and the test will fail +           return headers['Authorization'] == 'xxx'; +         }).respond(201, ''); -     var controller = scope.$new(MyController); -     controller.saveMessage('whatever'); -     $httpBackend.flush(); -   }); +         $rootScope.saveMessage('whatever'); +         $httpBackend.flush(); +       }); +    });     </pre>   */  angular.mock.$HttpBackendProvider = function() {  | 
