Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 296074f

Browse files
docs(ngMock/$httpBackend): fix testing example
Closes #3075
1 parent 2ccfaff commit 296074f

File tree

1 file changed

+81
-56
lines changed

1 file changed

+81
-56
lines changed

src/ngMock/angular-mocks.js

+81-56
Original file line numberDiff line numberDiff line change
@@ -773,75 +773,100 @@ angular.mock.dump = function(object) {
773773
*
774774
*
775775
* # Unit testing with mock $httpBackend
776+
* The following code shows how to setup and use the mock backend in unit testing a controller.
777+
* First we create the controller under test
776778
*
777-
* <pre>
778-
// controller
779-
function MyController($scope, $http) {
780-
$http.get('/auth.py').success(function(data) {
781-
$scope.user = data;
782-
});
783-
784-
this.saveMessage = function(message) {
785-
$scope.status = 'Saving...';
786-
$http.post('/add-msg.py', message).success(function(response) {
787-
$scope.status = '';
788-
}).error(function() {
789-
$scope.status = 'ERROR!';
790-
});
791-
};
792-
}
793-
794-
// testing controller
795-
var $httpBackend;
779+
<pre>
780+
// The controller code
781+
function MyController($scope, $http) {
782+
var authToken;
796783
797-
beforeEach(inject(function($injector) {
798-
$httpBackend = $injector.get('$httpBackend');
784+
$http.get('/auth.py').success(function(data, status, headers) {
785+
authToken = headers('A-Token');
786+
$scope.user = data;
787+
});
799788
800-
// backend definition common for all tests
801-
$httpBackend.when('GET', '/auth.py').respond({userId: 'userX'}, {'A-Token': 'xxx'});
802-
}));
789+
$scope.saveMessage = function(message) {
790+
var headers = { 'Authorization': authToken };
791+
$scope.status = 'Saving...';
803792
793+
$http.post('/add-msg.py', message, { headers: headers } ).success(function(response) {
794+
$scope.status = '';
795+
}).error(function() {
796+
$scope.status = 'ERROR!';
797+
});
798+
};
799+
}
800+
</pre>
801+
*
802+
* Now we setup the mock backend and create the test specs.
803+
*
804+
<pre>
805+
// testing controller
806+
describe('MyController', function() {
807+
var $httpBackend, $rootScope, createController;
808+
809+
beforeEach(inject(function($injector) {
810+
// Set up the mock http service responses
811+
$httpBackend = $injector.get('$httpBackend');
812+
// backend definition common for all tests
813+
$httpBackend.when('GET', '/auth.py').respond({userId: 'userX'}, {'A-Token': 'xxx'});
814+
815+
// Get hold of a scope (i.e. the root scope)
816+
$rootScope = $injector.get('$rootScope');
817+
// The $controller service is used to create instances of controllers
818+
var $controller = $injector.get('$controller');
819+
820+
createController = function() {
821+
return $controller('MyController', {'$scope' : $rootScope });
822+
};
823+
}));
824+
825+
826+
afterEach(function() {
827+
$httpBackend.verifyNoOutstandingExpectation();
828+
$httpBackend.verifyNoOutstandingRequest();
829+
});
804830
805-
afterEach(function() {
806-
$httpBackend.verifyNoOutstandingExpectation();
807-
$httpBackend.verifyNoOutstandingRequest();
808-
});
809831
832+
it('should fetch authentication token', function() {
833+
$httpBackend.expectGET('/auth.py');
834+
var controller = createController();
835+
$httpBackend.flush();
836+
});
810837
811-
it('should fetch authentication token', function() {
812-
$httpBackend.expectGET('/auth.py');
813-
var controller = scope.$new(MyController);
814-
$httpBackend.flush();
815-
});
816838
839+
it('should send msg to server', function() {
840+
var controller = createController();
841+
$httpBackend.flush();
817842
818-
it('should send msg to server', function() {
819-
// now you don’t care about the authentication, but
820-
// the controller will still send the request and
821-
// $httpBackend will respond without you having to
822-
// specify the expectation and response for this request
823-
$httpBackend.expectPOST('/add-msg.py', 'message content').respond(201, '');
843+
// now you don’t care about the authentication, but
844+
// the controller will still send the request and
845+
// $httpBackend will respond without you having to
846+
// specify the expectation and response for this request
824847
825-
var controller = scope.$new(MyController);
826-
$httpBackend.flush();
827-
controller.saveMessage('message content');
828-
expect(controller.status).toBe('Saving...');
829-
$httpBackend.flush();
830-
expect(controller.status).toBe('');
831-
});
848+
$httpBackend.expectPOST('/add-msg.py', 'message content').respond(201, '');
849+
$rootScope.saveMessage('message content');
850+
expect($rootScope.status).toBe('Saving...');
851+
$httpBackend.flush();
852+
expect($rootScope.status).toBe('');
853+
});
832854
833855
834-
it('should send auth header', function() {
835-
$httpBackend.expectPOST('/add-msg.py', undefined, function(headers) {
836-
// check if the header was send, if it wasn't the expectation won't
837-
// match the request and the test will fail
838-
return headers['Authorization'] == 'xxx';
839-
}).respond(201, '');
856+
it('should send auth header', function() {
857+
var controller = createController();
858+
$httpBackend.flush();
859+
860+
$httpBackend.expectPOST('/add-msg.py', undefined, function(headers) {
861+
// check if the header was send, if it wasn't the expectation won't
862+
// match the request and the test will fail
863+
return headers['Authorization'] == 'xxx';
864+
}).respond(201, '');
840865
841-
var controller = scope.$new(MyController);
842-
controller.saveMessage('whatever');
843-
$httpBackend.flush();
844-
});
866+
$rootScope.saveMessage('whatever');
867+
$httpBackend.flush();
868+
});
869+
});
845870
</pre>
846871
*/
847872
angular.mock.$HttpBackendProvider = function() {

0 commit comments

Comments
 (0)