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

Commit 477626d

Browse files
shahatapetebacondarwin
authored andcommitted
feat(ngMock): allow override of when/expect definitions
Closes #5766 Closes #8352
1 parent 25a476e commit 477626d

File tree

2 files changed

+136
-33
lines changed

2 files changed

+136
-33
lines changed

src/ngMock/angular-mocks.js

+79-33
Original file line numberDiff line numberDiff line change
@@ -1001,13 +1001,14 @@ angular.mock.dump = function(object) {
10011001
```js
10021002
// testing controller
10031003
describe('MyController', function() {
1004-
var $httpBackend, $rootScope, createController;
1004+
var $httpBackend, $rootScope, createController, authRequestHandler;
10051005
10061006
beforeEach(inject(function($injector) {
10071007
// Set up the mock http service responses
10081008
$httpBackend = $injector.get('$httpBackend');
10091009
// backend definition common for all tests
1010-
$httpBackend.when('GET', '/auth.py').respond({userId: 'userX'}, {'A-Token': 'xxx'});
1010+
authRequestHandler = $httpBackend.when('GET', '/auth.py')
1011+
.respond({userId: 'userX'}, {'A-Token': 'xxx'});
10111012
10121013
// Get hold of a scope (i.e. the root scope)
10131014
$rootScope = $injector.get('$rootScope');
@@ -1033,6 +1034,18 @@ angular.mock.dump = function(object) {
10331034
});
10341035
10351036
1037+
it('should fail authentication', function() {
1038+
1039+
// Notice how you can change the response even after it was set
1040+
authRequestHandler.respond(401, '');
1041+
1042+
$httpBackend.expectGET('/auth.py');
1043+
var controller = createController();
1044+
$httpBackend.flush();
1045+
expect($rootScope.status).toBe('Failed...');
1046+
});
1047+
1048+
10361049
it('should send msg to server', function() {
10371050
var controller = createController();
10381051
$httpBackend.flush();
@@ -1187,26 +1200,32 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
11871200
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
11881201
* object and returns true if the headers match the current definition.
11891202
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
1190-
* request is handled.
1203+
* request is handled. You can save this object for later use and invoke `respond` again in
1204+
* order to change how a matched request is handled.
11911205
*
11921206
* - respond –
11931207
* `{function([status,] data[, headers, statusText])
11941208
* | function(function(method, url, data, headers)}`
11951209
* – The respond method takes a set of static data to be returned or a function that can
11961210
* return an array containing response status (number), response data (string), response
1197-
* headers (Object), and the text for the status (string).
1211+
* headers (Object), and the text for the status (string). The respond method returns the
1212+
* `requestHandler` object for possible overrides.
11981213
*/
11991214
$httpBackend.when = function(method, url, data, headers) {
12001215
var definition = new MockHttpExpectation(method, url, data, headers),
12011216
chain = {
12021217
respond: function(status, data, headers, statusText) {
1218+
definition.passThrough = undefined;
12031219
definition.response = createResponse(status, data, headers, statusText);
1220+
return chain;
12041221
}
12051222
};
12061223

12071224
if ($browser) {
12081225
chain.passThrough = function() {
1226+
definition.response = undefined;
12091227
definition.passThrough = true;
1228+
return chain;
12101229
};
12111230
}
12121231

@@ -1224,7 +1243,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12241243
* and returns true if the url match the current definition.
12251244
* @param {(Object|function(Object))=} headers HTTP headers.
12261245
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1227-
* request is handled.
1246+
* request is handled. You can save this object for later use and invoke `respond` again in
1247+
* order to change how a matched request is handled.
12281248
*/
12291249

12301250
/**
@@ -1237,7 +1257,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12371257
* and returns true if the url match the current definition.
12381258
* @param {(Object|function(Object))=} headers HTTP headers.
12391259
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1240-
* request is handled.
1260+
* request is handled. You can save this object for later use and invoke `respond` again in
1261+
* order to change how a matched request is handled.
12411262
*/
12421263

12431264
/**
@@ -1250,7 +1271,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12501271
* and returns true if the url match the current definition.
12511272
* @param {(Object|function(Object))=} headers HTTP headers.
12521273
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1253-
* request is handled.
1274+
* request is handled. You can save this object for later use and invoke `respond` again in
1275+
* order to change how a matched request is handled.
12541276
*/
12551277

12561278
/**
@@ -1265,7 +1287,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12651287
* data string and returns true if the data is as expected.
12661288
* @param {(Object|function(Object))=} headers HTTP headers.
12671289
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1268-
* request is handled.
1290+
* request is handled. You can save this object for later use and invoke `respond` again in
1291+
* order to change how a matched request is handled.
12691292
*/
12701293

12711294
/**
@@ -1280,7 +1303,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12801303
* data string and returns true if the data is as expected.
12811304
* @param {(Object|function(Object))=} headers HTTP headers.
12821305
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1283-
* request is handled.
1306+
* request is handled. You can save this object for later use and invoke `respond` again in
1307+
* order to change how a matched request is handled.
12841308
*/
12851309

12861310
/**
@@ -1292,7 +1316,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12921316
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
12931317
* and returns true if the url match the current definition.
12941318
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1295-
* request is handled.
1319+
* request is handled. You can save this object for later use and invoke `respond` again in
1320+
* order to change how a matched request is handled.
12961321
*/
12971322
createShortMethods('when');
12981323

@@ -1312,23 +1337,28 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13121337
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
13131338
* object and returns true if the headers match the current expectation.
13141339
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1315-
* request is handled.
1340+
* request is handled. You can save this object for later use and invoke `respond` again in
1341+
* order to change how a matched request is handled.
13161342
*
13171343
* - respond –
13181344
* `{function([status,] data[, headers, statusText])
13191345
* | function(function(method, url, data, headers)}`
13201346
* – The respond method takes a set of static data to be returned or a function that can
13211347
* return an array containing response status (number), response data (string), response
1322-
* headers (Object), and the text for the status (string).
1348+
* headers (Object), and the text for the status (string). The respond method returns the
1349+
* `requestHandler` object for possible overrides.
13231350
*/
13241351
$httpBackend.expect = function(method, url, data, headers) {
1325-
var expectation = new MockHttpExpectation(method, url, data, headers);
1352+
var expectation = new MockHttpExpectation(method, url, data, headers),
1353+
chain = {
1354+
respond: function (status, data, headers, statusText) {
1355+
expectation.response = createResponse(status, data, headers, statusText);
1356+
return chain;
1357+
}
1358+
};
1359+
13261360
expectations.push(expectation);
1327-
return {
1328-
respond: function (status, data, headers, statusText) {
1329-
expectation.response = createResponse(status, data, headers, statusText);
1330-
}
1331-
};
1361+
return chain;
13321362
};
13331363

13341364

@@ -1342,7 +1372,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13421372
* and returns true if the url match the current definition.
13431373
* @param {Object=} headers HTTP headers.
13441374
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1345-
* request is handled. See #expect for more info.
1375+
* request is handled. You can save this object for later use and invoke `respond` again in
1376+
* order to change how a matched request is handled. See #expect for more info.
13461377
*/
13471378

13481379
/**
@@ -1355,7 +1386,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13551386
* and returns true if the url match the current definition.
13561387
* @param {Object=} headers HTTP headers.
13571388
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1358-
* request is handled.
1389+
* request is handled. You can save this object for later use and invoke `respond` again in
1390+
* order to change how a matched request is handled.
13591391
*/
13601392

13611393
/**
@@ -1368,7 +1400,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13681400
* and returns true if the url match the current definition.
13691401
* @param {Object=} headers HTTP headers.
13701402
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1371-
* request is handled.
1403+
* request is handled. You can save this object for later use and invoke `respond` again in
1404+
* order to change how a matched request is handled.
13721405
*/
13731406

13741407
/**
@@ -1384,7 +1417,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13841417
* is in JSON format.
13851418
* @param {Object=} headers HTTP headers.
13861419
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1387-
* request is handled.
1420+
* request is handled. You can save this object for later use and invoke `respond` again in
1421+
* order to change how a matched request is handled.
13881422
*/
13891423

13901424
/**
@@ -1400,7 +1434,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
14001434
* is in JSON format.
14011435
* @param {Object=} headers HTTP headers.
14021436
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1403-
* request is handled.
1437+
* request is handled. You can save this object for later use and invoke `respond` again in
1438+
* order to change how a matched request is handled.
14041439
*/
14051440

14061441
/**
@@ -1416,7 +1451,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
14161451
* is in JSON format.
14171452
* @param {Object=} headers HTTP headers.
14181453
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1419-
* request is handled.
1454+
* request is handled. You can save this object for later use and invoke `respond` again in
1455+
* order to change how a matched request is handled.
14201456
*/
14211457

14221458
/**
@@ -1428,7 +1464,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
14281464
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
14291465
* and returns true if the url match the current definition.
14301466
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1431-
* request is handled.
1467+
* request is handled. You can save this object for later use and invoke `respond` again in
1468+
* order to change how a matched request is handled.
14321469
*/
14331470
createShortMethods('expect');
14341471

@@ -1838,7 +1875,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
18381875
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
18391876
* object and returns true if the headers match the current definition.
18401877
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1841-
* control how a matched request is handled.
1878+
* control how a matched request is handled. You can save this object for later use and invoke
1879+
* `respond` or `passThrough` again in order to change how a matched request is handled.
18421880
*
18431881
* - respond –
18441882
* `{function([status,] data[, headers, statusText])
@@ -1849,6 +1887,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
18491887
* - passThrough – `{function()}` – Any request matching a backend definition with
18501888
* `passThrough` handler will be passed through to the real backend (an XHR request will be made
18511889
* to the server.)
1890+
* - Both methods return the `requestHandler` object for possible overrides.
18521891
*/
18531892

18541893
/**
@@ -1862,7 +1901,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
18621901
* and returns true if the url match the current definition.
18631902
* @param {(Object|function(Object))=} headers HTTP headers.
18641903
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1865-
* control how a matched request is handled.
1904+
* control how a matched request is handled. You can save this object for later use and invoke
1905+
* `respond` or `passThrough` again in order to change how a matched request is handled.
18661906
*/
18671907

18681908
/**
@@ -1876,7 +1916,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
18761916
* and returns true if the url match the current definition.
18771917
* @param {(Object|function(Object))=} headers HTTP headers.
18781918
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1879-
* control how a matched request is handled.
1919+
* control how a matched request is handled. You can save this object for later use and invoke
1920+
* `respond` or `passThrough` again in order to change how a matched request is handled.
18801921
*/
18811922

18821923
/**
@@ -1890,7 +1931,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
18901931
* and returns true if the url match the current definition.
18911932
* @param {(Object|function(Object))=} headers HTTP headers.
18921933
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1893-
* control how a matched request is handled.
1934+
* control how a matched request is handled. You can save this object for later use and invoke
1935+
* `respond` or `passThrough` again in order to change how a matched request is handled.
18941936
*/
18951937

18961938
/**
@@ -1905,7 +1947,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
19051947
* @param {(string|RegExp)=} data HTTP request body.
19061948
* @param {(Object|function(Object))=} headers HTTP headers.
19071949
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1908-
* control how a matched request is handled.
1950+
* control how a matched request is handled. You can save this object for later use and invoke
1951+
* `respond` or `passThrough` again in order to change how a matched request is handled.
19091952
*/
19101953

19111954
/**
@@ -1920,7 +1963,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
19201963
* @param {(string|RegExp)=} data HTTP request body.
19211964
* @param {(Object|function(Object))=} headers HTTP headers.
19221965
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1923-
* control how a matched request is handled.
1966+
* control how a matched request is handled. You can save this object for later use and invoke
1967+
* `respond` or `passThrough` again in order to change how a matched request is handled.
19241968
*/
19251969

19261970
/**
@@ -1935,7 +1979,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
19351979
* @param {(string|RegExp)=} data HTTP request body.
19361980
* @param {(Object|function(Object))=} headers HTTP headers.
19371981
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1938-
* control how a matched request is handled.
1982+
* control how a matched request is handled. You can save this object for later use and invoke
1983+
* `respond` or `passThrough` again in order to change how a matched request is handled.
19391984
*/
19401985

19411986
/**
@@ -1948,7 +1993,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
19481993
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
19491994
* and returns true if the url match the current definition.
19501995
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1951-
* control how a matched request is handled.
1996+
* control how a matched request is handled. You can save this object for later use and invoke
1997+
* `respond` or `passThrough` again in order to change how a matched request is handled.
19521998
*/
19531999
angular.mock.e2e = {};
19542000
angular.mock.e2e.$httpBackendDecorator =

test/ngMock/angular-mocksSpec.js

+57
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,44 @@ describe('ngMock', function() {
11551155
expect(callback.argsForCall[0]).toEqual([200, 'first', '', '']);
11561156
expect(callback.argsForCall[1]).toEqual([200, 'second', '', '']);
11571157
});
1158+
1159+
it('should be able to override response of expect definition', function() {
1160+
var definition = hb.expect('GET', '/url1');
1161+
definition.respond('first');
1162+
definition.respond('second');
1163+
1164+
hb('GET', '/url1', null, callback);
1165+
hb.flush();
1166+
expect(callback).toHaveBeenCalledOnceWith(200, 'second', '', '');
1167+
});
1168+
1169+
it('should be able to override response of when definition', function() {
1170+
var definition = hb.when('GET', '/url1');
1171+
definition.respond('first');
1172+
definition.respond('second');
1173+
1174+
hb('GET', '/url1', null, callback);
1175+
hb.flush();
1176+
expect(callback).toHaveBeenCalledOnceWith(200, 'second', '', '');
1177+
});
1178+
1179+
it('should be able to override response of expect definition with chaining', function() {
1180+
var definition = hb.expect('GET', '/url1').respond('first');
1181+
definition.respond('second');
1182+
1183+
hb('GET', '/url1', null, callback);
1184+
hb.flush();
1185+
expect(callback).toHaveBeenCalledOnceWith(200, 'second', '', '');
1186+
});
1187+
1188+
it('should be able to override response of when definition with chaining', function() {
1189+
var definition = hb.when('GET', '/url1').respond('first');
1190+
definition.respond('second');
1191+
1192+
hb('GET', '/url1', null, callback);
1193+
hb.flush();
1194+
expect(callback).toHaveBeenCalledOnceWith(200, 'second', '', '');
1195+
});
11581196
});
11591197

11601198

@@ -1557,6 +1595,25 @@ describe('ngMockE2E', function() {
15571595
expect(realHttpBackend).toHaveBeenCalledOnceWith(
15581596
'GET', '/passThrough/23', null, callback, {}, null, true);
15591597
});
1598+
1599+
it('should be able to override a respond definition with passThrough', function() {
1600+
var definition = hb.when('GET', /\/passThrough\/.*/).respond('override me');
1601+
definition.passThrough();
1602+
hb('GET', '/passThrough/23', null, callback, {}, null, true);
1603+
1604+
expect(realHttpBackend).toHaveBeenCalledOnceWith(
1605+
'GET', '/passThrough/23', null, callback, {}, null, true);
1606+
});
1607+
1608+
it('should be able to override a respond definition with passThrough', inject(function($browser) {
1609+
var definition = hb.when('GET', /\/passThrough\/.*/).passThrough();
1610+
definition.respond('passThrough override');
1611+
hb('GET', '/passThrough/23', null, callback, {}, null, true);
1612+
$browser.defer.flush();
1613+
1614+
expect(realHttpBackend).not.toHaveBeenCalled();
1615+
expect(callback).toHaveBeenCalledOnceWith(200, 'passThrough override', '', '');
1616+
}));
15601617
});
15611618

15621619

0 commit comments

Comments
 (0)