mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
feat($httpBackend): add expect/when shortcut methods
This commit is contained in:
parent
a13b5ed3bc
commit
b911e303ec
2 changed files with 30 additions and 0 deletions
15
src/angular-mocks.js
vendored
15
src/angular-mocks.js
vendored
|
|
@ -671,6 +671,9 @@ angular.module.ngMock.$HttpBackendProvider = function() {
|
|||
};
|
||||
};
|
||||
|
||||
createShortMethods('when');
|
||||
|
||||
|
||||
$httpBackend.expect = function(method, url, data, headers) {
|
||||
var expectation = new MockHttpExpectation(method, url, data, headers);
|
||||
expectations.push(expectation);
|
||||
|
|
@ -681,6 +684,9 @@ angular.module.ngMock.$HttpBackendProvider = function() {
|
|||
};
|
||||
};
|
||||
|
||||
createShortMethods('expect');
|
||||
|
||||
|
||||
$httpBackend.flush = function(count) {
|
||||
if (!responses.length) throw Error('No pending request to flush !');
|
||||
|
||||
|
|
@ -715,6 +721,15 @@ angular.module.ngMock.$HttpBackendProvider = function() {
|
|||
};
|
||||
|
||||
return $httpBackend;
|
||||
|
||||
|
||||
function createShortMethods(prefix) {
|
||||
angular.forEach(['GET', 'PUT', 'POST', 'DELETE', 'PATCH', 'JSONP'], function(method) {
|
||||
$httpBackend[prefix + method] = function(url, data, headers) {
|
||||
return $httpBackend[prefix](method, url, data, headers)
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
|||
15
test/angular-mocksSpec.js
vendored
15
test/angular-mocksSpec.js
vendored
|
|
@ -750,6 +750,21 @@ describe('mocks', function() {
|
|||
});
|
||||
|
||||
|
||||
describe('expect/when shorcuts', function() {
|
||||
angular.forEach(['expect', 'when'], function(prefix) {
|
||||
angular.forEach(['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'JSONP'], function(method) {
|
||||
var shortcut = prefix + method;
|
||||
it('should provide ' + shortcut + ' shortcut method', function() {
|
||||
hb[shortcut]('/foo').respond('bar');
|
||||
hb(method, '/foo', undefined, callback);
|
||||
hb.flush();
|
||||
expect(callback).toHaveBeenCalledOnceWith(200, 'bar', '');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('MockHttpExpectation', function() {
|
||||
|
||||
it('should accept url as regexp', function() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue