mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
refactor(mock.$httpBackend): rename when().then() to when().respond()
This commit is contained in:
parent
e9f81b6631
commit
6290bd4587
4 changed files with 43 additions and 43 deletions
2
src/angular-mocks.js
vendored
2
src/angular-mocks.js
vendored
|
|
@ -639,7 +639,7 @@ angular.module.ngMock.$HttpBackendProvider = function() {
|
|||
var definition = new MockHttpExpectation(method, url, data, headers);
|
||||
definitions.push(definition);
|
||||
return {
|
||||
then: function(status, data, headers) {
|
||||
respond: function(status, data, headers) {
|
||||
definition.response = angular.isFunction(status) ? status : createResponse(status, data, headers);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ describe("resource", function() {
|
|||
it('should ignore slashes of undefinend parameters', inject(function($httpBackend) {
|
||||
var R = resource.route('/Path/:a/:b/:c');
|
||||
|
||||
$httpBackend.when('GET').then('{}');
|
||||
$httpBackend.when('GET').respond('{}');
|
||||
$httpBackend.expect('GET', '/Path');
|
||||
$httpBackend.expect('GET', '/Path/1');
|
||||
$httpBackend.expect('GET', '/Path/2/3');
|
||||
|
|
|
|||
62
test/angular-mocksSpec.js
vendored
62
test/angular-mocksSpec.js
vendored
|
|
@ -380,8 +380,8 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it('should respond with first matched definition', function() {
|
||||
hb.when('GET', '/url1').then(200, 'content', {});
|
||||
hb.when('GET', '/url1').then(201, 'another', {});
|
||||
hb.when('GET', '/url1').respond(200, 'content', {});
|
||||
hb.when('GET', '/url1').respond(201, 'another', {});
|
||||
|
||||
callback.andCallFake(function(status, response) {
|
||||
expect(status).toBe(200);
|
||||
|
|
@ -396,7 +396,7 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it('should throw error when unexpected request', function() {
|
||||
hb.when('GET', '/url1').then(200, 'content');
|
||||
hb.when('GET', '/url1').respond(200, 'content');
|
||||
expect(function() {
|
||||
hb('GET', '/xxx');
|
||||
}).toThrow('Unexpected request: GET /xxx');
|
||||
|
|
@ -404,9 +404,9 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it('should match headers if specified', function() {
|
||||
hb.when('GET', '/url', null, {'X': 'val1'}).then(201, 'content1');
|
||||
hb.when('GET', '/url', null, {'X': 'val2'}).then(202, 'content2');
|
||||
hb.when('GET', '/url').then(203, 'content3');
|
||||
hb.when('GET', '/url', null, {'X': 'val1'}).respond(201, 'content1');
|
||||
hb.when('GET', '/url', null, {'X': 'val2'}).respond(202, 'content2');
|
||||
hb.when('GET', '/url').respond(203, 'content3');
|
||||
|
||||
hb('GET', '/url', null, function(status, response) {
|
||||
expect(status).toBe(203);
|
||||
|
|
@ -428,8 +428,8 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it('should match data if specified', function() {
|
||||
hb.when('GET', '/a/b', '{a: true}').then(201, 'content1');
|
||||
hb.when('GET', '/a/b').then(202, 'content2');
|
||||
hb.when('GET', '/a/b', '{a: true}').respond(201, 'content1');
|
||||
hb.when('GET', '/a/b').respond(202, 'content2');
|
||||
|
||||
hb('GET', '/a/b', '{a: true}', function(status, response) {
|
||||
expect(status).toBe(201);
|
||||
|
|
@ -446,7 +446,7 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it('should match only method', function() {
|
||||
hb.when('GET').then(202, 'c');
|
||||
hb.when('GET').respond(202, 'c');
|
||||
callback.andCallFake(function(status, response) {
|
||||
expect(status).toBe(202);
|
||||
expect(response).toBe('c');
|
||||
|
|
@ -462,7 +462,7 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it('should expose given headers', function() {
|
||||
hb.when('GET', '/u1').then(200, null, {'X-Fake': 'Header', 'Content-Type': 'application/json'});
|
||||
hb.when('GET', '/u1').respond(200, null, {'X-Fake': 'Header', 'Content-Type': 'application/json'});
|
||||
var xhr = hb('GET', '/u1', null, noop, {});
|
||||
hb.flush();
|
||||
expect(xhr.getResponseHeader('X-Fake')).toBe('Header');
|
||||
|
|
@ -471,8 +471,8 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it('should preserve the order of requests', function() {
|
||||
hb.when('GET', '/url1').then(200, 'first');
|
||||
hb.when('GET', '/url2').then(201, 'second');
|
||||
hb.when('GET', '/url1').respond(200, 'first');
|
||||
hb.when('GET', '/url2').respond(201, 'second');
|
||||
|
||||
hb('GET', '/url2', null, callback);
|
||||
hb('GET', '/url1', null, callback);
|
||||
|
|
@ -485,8 +485,8 @@ describe('mocks', function() {
|
|||
});
|
||||
|
||||
|
||||
it('then() should take function', function() {
|
||||
hb.when('GET', '/some').then(function(m, u, d, h) {
|
||||
it('respond() should take function', function() {
|
||||
hb.when('GET', '/some').respond(function(m, u, d, h) {
|
||||
return [301, m + u + ';' + d + ';a=' + h.a, {'Connection': 'keep-alive'}];
|
||||
});
|
||||
|
||||
|
|
@ -516,7 +516,7 @@ describe('mocks', function() {
|
|||
expect(response).toBe('expect');
|
||||
});
|
||||
|
||||
hb.when('GET', '/url').then(200, 'when');
|
||||
hb.when('GET', '/url').respond(200, 'when');
|
||||
hb.expect('GET', '/url').respond(300, 'expect');
|
||||
|
||||
hb('GET', '/url', null, callback, {});
|
||||
|
|
@ -526,7 +526,7 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it ('should throw exception when only headers differes from expectation', function() {
|
||||
hb.when('GET').then(200, '', {});
|
||||
hb.when('GET').respond(200, '', {});
|
||||
hb.expect('GET', '/match', undefined, {'Content-Type': 'application/json'});
|
||||
|
||||
expect(function() {
|
||||
|
|
@ -537,7 +537,7 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it ('should throw exception when only data differes from expectation', function() {
|
||||
hb.when('GET').then(200, '', {});
|
||||
hb.when('GET').respond(200, '', {});
|
||||
hb.expect('GET', '/match', 'some-data');
|
||||
|
||||
expect(function() {
|
||||
|
|
@ -547,13 +547,13 @@ describe('mocks', function() {
|
|||
});
|
||||
|
||||
|
||||
it('expect() should without respond() and use then()', function() {
|
||||
it('expect() should without respond() and use respond()', function() {
|
||||
callback.andCallFake(function(status, response) {
|
||||
expect(status).toBe(201);
|
||||
expect(response).toBe('data');
|
||||
});
|
||||
|
||||
hb.when('GET', '/some').then(201, 'data');
|
||||
hb.when('GET', '/some').respond(201, 'data');
|
||||
hb.expect('GET', '/some');
|
||||
hb('GET', '/some', null, callback);
|
||||
hb.flush();
|
||||
|
|
@ -564,7 +564,7 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it('flush() should flush requests fired during callbacks', function() {
|
||||
hb.when('GET').then(200, '');
|
||||
hb.when('GET').respond(200, '');
|
||||
hb('GET', '/some', null, function() {
|
||||
hb('GET', '/other', null, callback);
|
||||
});
|
||||
|
|
@ -575,7 +575,7 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it('flush() should flush given number of pending requests', function() {
|
||||
hb.when('GET').then(200, '');
|
||||
hb.when('GET').respond(200, '');
|
||||
hb('GET', '/some', null, callback);
|
||||
hb('GET', '/some', null, callback);
|
||||
hb('GET', '/some', null, callback);
|
||||
|
|
@ -587,7 +587,7 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it('flush() should throw exception when flushing more requests than pending', function() {
|
||||
hb.when('GET').then(200, '');
|
||||
hb.when('GET').respond(200, '');
|
||||
hb('GET', '/url', null, callback);
|
||||
|
||||
expect(function() {hb.flush(2);}).toThrow('No more pending request to flush !');
|
||||
|
|
@ -598,7 +598,7 @@ describe('mocks', function() {
|
|||
it('(flush) should throw exception when no request to flush', function() {
|
||||
expect(function() {hb.flush();}).toThrow('No pending request to flush !');
|
||||
|
||||
hb.when('GET').then(200, '');
|
||||
hb.when('GET').respond(200, '');
|
||||
hb('GET', '/some', null, callback);
|
||||
hb.flush();
|
||||
|
||||
|
|
@ -622,14 +622,14 @@ describe('mocks', function() {
|
|||
});
|
||||
|
||||
|
||||
it('then() should set default status 200 if not defined', function() {
|
||||
it('respond() should set default status 200 if not defined', function() {
|
||||
callback.andCallFake(function(status, response) {
|
||||
expect(status).toBe(200);
|
||||
expect(response).toBe('some-data');
|
||||
});
|
||||
|
||||
hb.when('GET', '/url1').then('some-data');
|
||||
hb.when('GET', '/url2').then('some-data', {'X-Header': 'true'});
|
||||
hb.when('GET', '/url1').respond('some-data');
|
||||
hb.when('GET', '/url2').respond('some-data', {'X-Header': 'true'});
|
||||
hb('GET', '/url1', null, callback);
|
||||
hb('GET', '/url2', null, callback);
|
||||
hb.flush();
|
||||
|
|
@ -644,7 +644,7 @@ describe('mocks', function() {
|
|||
expect(response).toBe('def-response');
|
||||
});
|
||||
|
||||
hb.when('GET').then(201, 'def-response');
|
||||
hb.when('GET').respond(201, 'def-response');
|
||||
hb.expect('GET', '/some-url');
|
||||
|
||||
hb('GET', '/some-url', null, callback);
|
||||
|
|
@ -671,7 +671,7 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it('should respond undefined when JSONP method', function() {
|
||||
hb.when('JSONP', '/url1').then(200);
|
||||
hb.when('JSONP', '/url1').respond(200);
|
||||
hb.expect('JSONP', '/url2').respond(200);
|
||||
|
||||
expect(hb('JSONP', '/url1')).toBeUndefined();
|
||||
|
|
@ -694,7 +694,7 @@ describe('mocks', function() {
|
|||
|
||||
|
||||
it('should do nothing when no expectation', function() {
|
||||
hb.when('DELETE', '/some').then(200, '');
|
||||
hb.when('DELETE', '/some').respond(200, '');
|
||||
|
||||
expect(function() {hb.verifyNoOutstandingExpectation();}).not.toThrow();
|
||||
});
|
||||
|
|
@ -703,7 +703,7 @@ describe('mocks', function() {
|
|||
it('should do nothing when all expectations satisfied', function() {
|
||||
hb.expect('GET', '/u2').respond(200, '', {});
|
||||
hb.expect('POST', '/u3').respond(201, '', {});
|
||||
hb.when('DELETE', '/some').then(200, '');
|
||||
hb.when('DELETE', '/some').respond(200, '');
|
||||
|
||||
hb('GET', '/u2', noop);
|
||||
hb('POST', '/u3', noop);
|
||||
|
|
@ -715,7 +715,7 @@ describe('mocks', function() {
|
|||
describe('verifyRequests', function() {
|
||||
|
||||
it('should throw exception if not all requests were flushed', function() {
|
||||
hb.when('GET').then(200);
|
||||
hb.when('GET').respond(200);
|
||||
hb('GET', '/some', null, noop, {});
|
||||
|
||||
expect(function() {
|
||||
|
|
|
|||
|
|
@ -411,7 +411,7 @@ describe('$http', function() {
|
|||
var future, rawXhrObject;
|
||||
|
||||
beforeEach(function() {
|
||||
$httpBackend.when('GET', '/url').then('');
|
||||
$httpBackend.when('GET', '/url').respond('');
|
||||
future = $http({method: 'GET', url: '/url'});
|
||||
rawXhrObject = MockXhr.$$lastInstance;
|
||||
spyOn(rawXhrObject, 'abort');
|
||||
|
|
@ -490,7 +490,7 @@ describe('$http', function() {
|
|||
}
|
||||
|
||||
beforeEach(function() {
|
||||
$httpBackend.when('GET').then(function(m, url) {
|
||||
$httpBackend.when('GET').respond(function(m, url) {
|
||||
return [parseInt(url.substr(1)), '', {}];
|
||||
});
|
||||
});
|
||||
|
|
@ -658,7 +658,7 @@ describe('$http', function() {
|
|||
describe('scope.$apply', function() {
|
||||
|
||||
it('should $apply after success callback', function() {
|
||||
$httpBackend.when('GET').then(200);
|
||||
$httpBackend.when('GET').respond(200);
|
||||
$http({method: 'GET', url: '/some'});
|
||||
$httpBackend.flush();
|
||||
expect(scope.$apply).toHaveBeenCalledOnce();
|
||||
|
|
@ -666,7 +666,7 @@ describe('$http', function() {
|
|||
|
||||
|
||||
it('should $apply after error callback', function() {
|
||||
$httpBackend.when('GET').then(404);
|
||||
$httpBackend.when('GET').respond(404);
|
||||
$http({method: 'GET', url: '/some'});
|
||||
$httpBackend.flush();
|
||||
expect(scope.$apply).toHaveBeenCalledOnce();
|
||||
|
|
@ -674,7 +674,7 @@ describe('$http', function() {
|
|||
|
||||
|
||||
it('should $apply even if exception thrown during callback', function() {
|
||||
$httpBackend.when('GET').then(200);
|
||||
$httpBackend.when('GET').respond(200);
|
||||
callback.andThrow('error in callback');
|
||||
|
||||
$http({method: 'GET', url: '/some'}).on('200', callback);
|
||||
|
|
@ -687,7 +687,7 @@ describe('$http', function() {
|
|||
|
||||
|
||||
it('should broadcast $http.request', function() {
|
||||
$httpBackend.when('GET').then(200);
|
||||
$httpBackend.when('GET').respond(200);
|
||||
scope.$on('$http.request', callback);
|
||||
var xhrFuture = $http({method: 'GET', url: '/whatever'});
|
||||
|
||||
|
|
@ -880,7 +880,7 @@ describe('$http', function() {
|
|||
describe('pendingRequests', function() {
|
||||
|
||||
it('should be an array of pending requests', function() {
|
||||
$httpBackend.when('GET').then(200);
|
||||
$httpBackend.when('GET').respond(200);
|
||||
expect($http.pendingRequests.length).toBe(0);
|
||||
|
||||
$http({method: 'get', url: '/some'});
|
||||
|
|
@ -892,7 +892,7 @@ describe('$http', function() {
|
|||
|
||||
|
||||
it('should remove the request when aborted', function() {
|
||||
$httpBackend.when('GET').then(0);
|
||||
$httpBackend.when('GET').respond(0);
|
||||
future = $http({method: 'get', url: '/x'});
|
||||
expect($http.pendingRequests.length).toBe(1);
|
||||
|
||||
|
|
@ -904,7 +904,7 @@ describe('$http', function() {
|
|||
|
||||
|
||||
it('should remove the request when served from cache', function() {
|
||||
$httpBackend.when('GET').then(200);
|
||||
$httpBackend.when('GET').respond(200);
|
||||
|
||||
$http({method: 'get', url: '/cached', cache: true});
|
||||
$httpBackend.flush();
|
||||
|
|
@ -919,7 +919,7 @@ describe('$http', function() {
|
|||
|
||||
|
||||
it('should remove the request before firing callbacks', function() {
|
||||
$httpBackend.when('GET').then(200);
|
||||
$httpBackend.when('GET').respond(200);
|
||||
$http({method: 'get', url: '/url'}).on('xxx', function() {
|
||||
expect($http.pendingRequests.length).toBe(0);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue