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 Pro
I make two minor bug fixes in mock module to make it works.
The updated mockedRest.js
:
exports.apiMockModule = function () {
console.log('apiMockModule executing');
var serviceId = 'mockedApiInterceptor';
angular.module('apiMockModule', [])
.config(['$httpProvider', configApiMock])
.factory(serviceId,
[mockedApiInterceptor]);
function mockedApiInterceptor() {
return {
request: function (config) {
console.log('apiMockModule intercepted');
if ((config.url.indexOf('api')) > -1) {
config.url = config.url.replace('api/', 'apiMock/');
}
return config;
},
response: function (response) {
return response
}
};
}
function configApiMock($httpProvider) {
$httpProvider.interceptors.push('mockedApiInterceptor');
}
};
I have tested this code in this environment:
You wrote:
There isn't anything logged to console inside the apiMockModule
It is normal, the module code is not executed by protractor, but sent to the browser (using driver.executeScript). So the code is executed by the browser.
But it is possible to get the logs from the browser for debugging:
...
it('tests the new apiMock', function() {
browser.manage().logs().get('browser').then(function(browserLog) {
console.log('log: ' + require('util').inspect(browserLog));
});
...