Skip to content

Commit 0dae350

Browse files
author
Carlos Atencio
committed
Dynamic add and remove functions for mock expectations
1 parent b03256c commit 0dae350

File tree

6 files changed

+217
-2
lines changed

6 files changed

+217
-2
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,44 @@ For testing or debugging purposes, it is possible to extract a list of http requ
164164
]);
165165

166166
It is also possible to clear the list of requests with the `clearRequests()` method.
167-
167+
168+
### Runtime mocks
169+
If there is a need to add or remove mocks during test execution, please use the `add()` and `remove()` functions:
170+
171+
mock.add([{
172+
request: {
173+
path: '/users',
174+
method: 'GET',
175+
params: {
176+
name: 'Charlie'
177+
}
178+
},
179+
response: {
180+
data: {
181+
name: 'Override'
182+
}
183+
}
184+
}]);
185+
186+
...
187+
188+
mock.remove([{
189+
request: {
190+
path: '/users',
191+
method: 'GET',
192+
params: {
193+
name: 'Charlie'
194+
}
195+
},
196+
response: {
197+
data: {
198+
name: 'Override'
199+
}
200+
}
201+
}]);
202+
203+
These will dynamically modify your current set of mocks, and any new request that happens after that will work with the updated set of mocks. Please note that these functions only work by adding or removing mocks using inline objects. As of now, it is not possible to add or remove mocks using mock files.
204+
168205
### Examples
169206
Included in the code base is an extensive list examples on how to use all the features of this plugin. Please take a look if you have any questions.
170207

example/spec/runtime.spec.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
var mock = require('../../index'), //substitute for require('protractor-http-mock')
2+
get = require('./get');
3+
4+
describe('inline', function(){
5+
6+
beforeEach(function(){
7+
mock([
8+
{
9+
request: {
10+
path: '/users',
11+
method: 'GET'
12+
},
13+
response: {
14+
data: [
15+
{
16+
firstName: 'default',
17+
lastName: 'value'
18+
}
19+
]
20+
}
21+
},
22+
{
23+
request: {
24+
path: '/users',
25+
method: 'GET',
26+
params: {
27+
name: 'Charlie'
28+
}
29+
},
30+
response: {
31+
data: {
32+
name: 'Setup'
33+
}
34+
}
35+
}
36+
]);
37+
38+
get();
39+
40+
mock.add([{
41+
request: {
42+
path: '/users',
43+
method: 'GET',
44+
params: {
45+
name: 'Charlie'
46+
}
47+
},
48+
response: {
49+
data: {
50+
name: 'Override'
51+
}
52+
}
53+
}]);
54+
});
55+
56+
afterEach(function(){
57+
mock.teardown();
58+
});
59+
60+
it('can add mocks', function(){
61+
element(by.id('user-query')).clear().sendKeys('Charlie');
62+
element(by.id('user-search-button')).click();
63+
64+
expect(element(by.id('user-data')).getText()).toContain('Override');
65+
});
66+
67+
it('can remove mocks', function(){
68+
mock.remove([{
69+
request: {
70+
path: '/users',
71+
method: 'GET',
72+
params: {
73+
name: 'Charlie'
74+
}
75+
},
76+
response: {
77+
data: {
78+
name: 'Override'
79+
}
80+
}
81+
}]);
82+
83+
element(by.id('user-query')).clear().sendKeys('Charlie');
84+
element(by.id('user-search-button')).click();
85+
86+
expect(element(by.id('user-data')).getText()).toContain('Setup');
87+
});
88+
});

lib/httpMock.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,20 @@ function mockTemplate() {
326326
newModule.requests = [];
327327
};
328328

329+
newModule.addMocks = function(expectationsToAdd){
330+
expectations = expectations.concat(expectationsToAdd);
331+
};
332+
333+
newModule.removeMocks = function(expectationsToRemove){
334+
expectations.forEach(function(expectation, index) {
335+
expectationsToRemove.forEach(function(expectationToRemove) {
336+
if (angular.equals(expectationToRemove, expectation)) {
337+
expectations.splice(index, 1);
338+
}
339+
});
340+
});
341+
};
342+
329343
return newModule;
330344
}
331345

lib/initData.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,20 @@ module.exports.clearRequests = function(){
9090
var callback = arguments[arguments.length - 1]
9191
callback(true);
9292
});
93+
};
94+
95+
module.exports.add = function(mocks){
96+
return browser.executeAsyncScript(function () {
97+
angular.module("httpMock").addMocks(arguments[0]);
98+
var callback = arguments[arguments.length - 1]
99+
callback(true);
100+
}, mocks);
101+
};
102+
103+
module.exports.remove = function(mocks){
104+
return browser.executeAsyncScript(function () {
105+
angular.module("httpMock").removeMocks(arguments[0]);
106+
var callback = arguments[arguments.length - 1]
107+
callback(JSON.stringify(true));
108+
}, mocks);
93109
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "protractor-http-mock",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "Mock HTTP calls in your protractor specs.",
55
"main": "index.js",
66
"scripts": {

tests/httpMock.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,4 +746,64 @@ describe('http mock', function(){
746746
});
747747
});
748748
});
749+
750+
describe('runtime mocks', function(){
751+
var runtimeMocks = [
752+
{
753+
request: {
754+
method: 'GET',
755+
path: '/runtime'
756+
},
757+
response: {
758+
data: 'runtime'
759+
}
760+
},
761+
{
762+
request: {
763+
method: 'GET',
764+
path: '/runtime'
765+
},
766+
response: {
767+
data: 'override'
768+
}
769+
}
770+
];
771+
772+
afterEach(function(){
773+
module.removeMocks(runtimeMocks);
774+
});
775+
776+
it('adds runtime mocks', function(done){
777+
module.addMocks(runtimeMocks);
778+
779+
http({
780+
method: 'GET',
781+
url: '/runtime'
782+
}).then(function(response){
783+
expect(response.data).toBe('override');
784+
done();
785+
});
786+
});
787+
788+
it('can remove runtime mocks', function(done){
789+
module.addMocks(runtimeMocks);
790+
module.removeMocks([{
791+
request: {
792+
method: 'GET',
793+
path: '/runtime'
794+
},
795+
response: {
796+
data: 'override'
797+
}
798+
}]);
799+
800+
http({
801+
method: 'GET',
802+
url: '/runtime'
803+
}).then(function(response){
804+
expect(response.data).toBe('runtime');
805+
done();
806+
});
807+
});
808+
});
749809
});

0 commit comments

Comments
 (0)