在将依赖项升级到最新的angular.js
版本( 1.7.3 )期间,所有单元测试都将停止工作。
到目前为止,我能够将问题减少到模拟设置中,如下所示:
my.module.mocks.js 的文件内容:
console.log('This logging appears regardless of the version'); // YES
angular.module('my.module.mocks-testing', ['my.module', 'ngMock']).run(initMockData);
angular.module('my.module.mocks', ['my.module', 'ngMockE2E']).run(initMockData);
// Function is never executed since 1.7 upgrade
function initMockData($httpBackend) {
console.log('initMockData function has been executed'); // NO it hasn't since 1.7.x
$httpBackend
.whenRoute('GET', '/gate/products/:id')
.respond(function (method, url, data, headers, params) {
var productExists = (PRODUCTS_SERVICE_RESPONSE[params.id.toString()] !== undefined),
successfulResponse = [200, PRODUCTS_SERVICE_RESPONSE[params.id.toString()]],
errorResponse = [403, {message: 'Forbidden'}];
return (productExists) ? successfulResponse : errorResponse;
});
$httpBackend
.whenRoute('GET', '/gate/companion-product-groups/:uid')
.respond(function (method, url, data, headers, params) {
var companionProductGroupExists = (COMPANIONS_SERVICE_RESPONSE[params.uid.toString()] !== undefined),
successfulResponse = [200, COMPANIONS_SERVICE_RESPONSE[params.uid.toString()]],
errorResponse = [403, {message: 'Forbidden'}];
return (companionProductGroupExists) ? successfulResponse : errorResponse;
});
}
搜索有角upgrade guide并没有任何帮助。
一旦我从*.mock.file
中移除了karma.conf.js
,Angular就会抛出预期的错误:
Error: [$injector:modulerr] Failed to instantiate module my.module.mocks-testing due to:
[$injector:nomod] Module 'my.module.mocks-testing' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
感谢您提供有助于解决问题的帮助