I'm mocking some end-points on an Angular website with Protractor using the following pattern:
// mock.js
// adding the mock module and passing it data
Mock.prototype.all = {
default: function() {
browser.addMockModule('httpBackendMock', mockModules.all, {
data: data
})
}
}
// mockmodules.js
// add dependencies to module and inject it
exports.all = function() {
$httpBackend.whenGET(/user/).respond(function() {
return [200, data.mockData.data];
});
$httpBackend.whenGET(/.*/).passThrough();
$httpBackend.whenPOST(/.*/).passThrough();
angular.module('httpBackendMock' ['ngMockE2E', 'sportsbook-app'])
.value('data', {
'mockData': arguments[0],
'count': 0
})
.run(runBlock);
runBlock.$inject = ['$httpBackend', 'data', 'eventDataSourceManager'];
}
// test-spec.js
// call mock from test
it("Mock", function() {
mock.all.default();
});
Now this works fine, but I want to be able to add more modules by having multiple modules that all send different data. Example:
Mock.prototype.all = {
default: function() {
browser.addMockModule('httpBackendMock', mockModules.allDefault, {
data: data
})
},
user: function() {
browser.addMockModule('user', mockModules.allUser, {
userData: userData
})
}
}
it("Mock", function() {
mock.all.default();
mock.all.user();
});
Like this, none of the mocks seem to be working.
Also within the selenium server I can see that both modules' names are being found:
Executing: [execute script: angular.resumeBootstrap(arguments[0]);, [[protractorBaseModule_, httpBackendMock, user]]])
At first I thought the issue was with
$httpBackend.whenGET(/.*/).passThrough();
$httpBackend.whenPOST(/.*/).passThrough();
Tried adding this to a separate module and call it after all other modules are called, but still it didn't work. Any ideas?
This is working as intended, because both of your mock modules use the same module name identifier, httpBackendMock
. You could use a unique name and then the latest module would not override the others.