emp*_*emp 8 angularjs angularjs-e2e protractor
这个问题是我的另一个问题的可能解决方案(他们建议使用addMockModule
量角器):使用Protractor运行测试时调用其他api.
我有以下文件:mockedRest.js
这是我要添加到量角器的模块.它应该拦截任何REST调用并替换地址(api /到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');
}
};
Run Code Online (Sandbox Code Playgroud)
然后我在实际测试中加载模块.
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.
});
});
Run Code Online (Sandbox Code Playgroud)
但是,REST调用仍然指向'api /'而不是'apiMock /'我不知道我是否还需要做更多的事情才能让拦截器完成它的工作.还值得注意的是,apiMockModule中没有任何记录到控制台,就像它没有加载模块一样.
任何建议表示赞赏.
gon*_*ard 11
我在模拟模块中做了两个小错误修复,使它工作.
更新的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');
}
};
Run Code Online (Sandbox Code Playgroud)
我已在此环境中测试了此代码:
你写了:
apiMockModule中没有任何记录到控制台的内容
这是正常的,模块代码不是由量角器执行,而是发送到浏览器(使用driver.executeScript
).所以代码由浏览器执行.
但是可以从浏览器获取日志以进行调试:
...
it('tests the new apiMock', function() {
browser.manage().logs().get('browser').then(function(browserLog) {
console.log('log: ' + require('util').inspect(browserLog));
});
...
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4945 次 |
最近记录: |