mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-25 22:33:44 +00:00
parent
2ccfaffa74
commit
296074f548
1 changed files with 84 additions and 59 deletions
141
src/ngMock/angular-mocks.js
vendored
141
src/ngMock/angular-mocks.js
vendored
|
|
@ -773,75 +773,100 @@ angular.mock.dump = function(object) {
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* # Unit testing with mock $httpBackend
|
* # 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>
|
<pre>
|
||||||
// controller
|
// The controller code
|
||||||
function MyController($scope, $http) {
|
function MyController($scope, $http) {
|
||||||
$http.get('/auth.py').success(function(data) {
|
var authToken;
|
||||||
$scope.user = data;
|
|
||||||
});
|
|
||||||
|
|
||||||
this.saveMessage = function(message) {
|
$http.get('/auth.py').success(function(data, status, headers) {
|
||||||
$scope.status = 'Saving...';
|
authToken = headers('A-Token');
|
||||||
$http.post('/add-msg.py', message).success(function(response) {
|
$scope.user = data;
|
||||||
$scope.status = '';
|
});
|
||||||
}).error(function() {
|
|
||||||
$scope.status = 'ERROR!';
|
$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();
|
||||||
});
|
});
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// testing controller
|
|
||||||
var $httpBackend;
|
|
||||||
|
|
||||||
beforeEach(inject(function($injector) {
|
|
||||||
$httpBackend = $injector.get('$httpBackend');
|
|
||||||
|
|
||||||
// backend definition common for all tests
|
|
||||||
$httpBackend.when('GET', '/auth.py').respond({userId: 'userX'}, {'A-Token': 'xxx'});
|
|
||||||
}));
|
|
||||||
|
|
||||||
|
|
||||||
afterEach(function() {
|
it('should fetch authentication token', function() {
|
||||||
$httpBackend.verifyNoOutstandingExpectation();
|
$httpBackend.expectGET('/auth.py');
|
||||||
$httpBackend.verifyNoOutstandingRequest();
|
var controller = createController();
|
||||||
});
|
$httpBackend.flush();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should fetch authentication token', function() {
|
it('should send msg to server', function() {
|
||||||
$httpBackend.expectGET('/auth.py');
|
var controller = createController();
|
||||||
var controller = scope.$new(MyController);
|
$httpBackend.flush();
|
||||||
$httpBackend.flush();
|
|
||||||
});
|
// 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, '');
|
||||||
|
$rootScope.saveMessage('message content');
|
||||||
|
expect($rootScope.status).toBe('Saving...');
|
||||||
|
$httpBackend.flush();
|
||||||
|
expect($rootScope.status).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should send msg to server', function() {
|
it('should send auth header', function() {
|
||||||
// now you don’t care about the authentication, but
|
var controller = createController();
|
||||||
// the controller will still send the request and
|
$httpBackend.flush();
|
||||||
// $httpBackend will respond without you having to
|
|
||||||
// specify the expectation and response for this request
|
|
||||||
$httpBackend.expectPOST('/add-msg.py', 'message content').respond(201, '');
|
|
||||||
|
|
||||||
var controller = scope.$new(MyController);
|
$httpBackend.expectPOST('/add-msg.py', undefined, function(headers) {
|
||||||
$httpBackend.flush();
|
// check if the header was send, if it wasn't the expectation won't
|
||||||
controller.saveMessage('message content');
|
// match the request and the test will fail
|
||||||
expect(controller.status).toBe('Saving...');
|
return headers['Authorization'] == 'xxx';
|
||||||
$httpBackend.flush();
|
}).respond(201, '');
|
||||||
expect(controller.status).toBe('');
|
|
||||||
});
|
|
||||||
|
|
||||||
|
$rootScope.saveMessage('whatever');
|
||||||
it('should send auth header', function() {
|
$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();
|
|
||||||
});
|
|
||||||
</pre>
|
</pre>
|
||||||
*/
|
*/
|
||||||
angular.mock.$HttpBackendProvider = function() {
|
angular.mock.$HttpBackendProvider = function() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue