This question is a possible solution for my other question (where they advice to use addMockModule
from protractor): Call other api when running tests using Protractor.
I have the following file: mockedRest.js
this is the module I want to add to protractor. It should intercept any REST calls and replace the address (api/ to apiMock/).
exports.apiMockModule = function () {
console.log('apiMockModule executing');
var serviceId = 'mockedApiInterceptor';
angular.module('apiMockModule', ['myApp'])
.config(['$httpProvider', configApiMock])
.factory(serviceId,
[mockedApiInterceptor]);
function mockedApiInterceptor() {
return {
request: function (config) {
console.log('apiMockModule intercepted');
if ((config.url.indexOf('api')) > -1) {
config.url.replace('api/', 'apiMock/');
}
return config;
},
response: function (response) {
return response
}
};
}
function configApiMock($httpProvider) {
$httpProvider.interceptors.push('mockedApiInterceptor');
}
};
Then I have my actual test where I load the module.
describe('E2E addMockModule', function() {
beforeEach(function() {
var mockModule = require('./mockedRest');
browser.addMockModule('apiMockModule', mockModule.apiMockModule);
console.log('apiMockModule loaded');
browser.get('#page');
});
it('tests the new apiMock', function() {
// test that clicks a button that performs a rest api call. left out as I can see the call in fiddler.
});
});
However, the REST call is still pointing to 'api/' instead of 'apiMock/' I don't know if I have to do anything more in order to get the interceptor to do its work. It's also worth to note that there isn't anything logged to console inside the apiMockModule, like it isn't loading the module.
Any advice is appreciated.