不知道如何测试 angular.bootstrap 包装器
Do not know how to test angular.bootstrap wrapper
我正在为我们的代码编写单元测试,我有一些使用 angular.bootstrap 的功能,但我不知道如何使用 karma 测试它,因为那时它不属于任何模块或任何东西
(function () {
'use strict';
function fetchData() {
var initInjector = angular.injector(['ng']);
var $http = initInjector.get('$http');
return $http.get('./app/config/app.config.json').then(function (response) {
angular.module('myModule').value('globalConfig', response.data);
return $http.get('./app/config/routes.config.json').then(function (response) {
angular.module('myModule').value('routesConfig', response.data);
}, function (errorResponse) {
// Handle error case
console.log('Error on loading routes:', errorResponse);
});
}, function (errorResponse) {
// Handle error case
console.log('Error on bootstrapping:', errorResponse);
});
}
function bootstrapApplication() {
angular.element(document).ready(function () {
angular.bootstrap(document, ['myModule']);
});
}
fetchData().then(bootstrapApplication);
}());
您可以将此块重构为属于常规 angular 服务,以便它可以测试。例如(我假设您的调用可以 运行 并行):
angular.module('myBootstrapModule', [])
.factory('bootstrap', function($q, $http) {
return function(moduleName) {
return $q.all([
$http.get('./app/config/app.config.json'),
$http.get('./app/config/routes.config.json')
])
.then(function(results) {
angular.module(moduleName)
.value('globalConfig', results[0].data)
.value('routesConfig', results[1].data);
})
.catch(function(res) {
// handle error
});
}
}
});
然后可以通过手动创建包含此模块的注入器来实现引导,而测试可以通过加载 myBootstrapModule
:
正常进行
var injector = angular.injector(['ng', 'myBootstrapModule']);
var bootstrap = injector.get('bootstrap');
bootstrap('myModule').then(function() {
// bootstrap angular
});
我正在为我们的代码编写单元测试,我有一些使用 angular.bootstrap 的功能,但我不知道如何使用 karma 测试它,因为那时它不属于任何模块或任何东西
(function () {
'use strict';
function fetchData() {
var initInjector = angular.injector(['ng']);
var $http = initInjector.get('$http');
return $http.get('./app/config/app.config.json').then(function (response) {
angular.module('myModule').value('globalConfig', response.data);
return $http.get('./app/config/routes.config.json').then(function (response) {
angular.module('myModule').value('routesConfig', response.data);
}, function (errorResponse) {
// Handle error case
console.log('Error on loading routes:', errorResponse);
});
}, function (errorResponse) {
// Handle error case
console.log('Error on bootstrapping:', errorResponse);
});
}
function bootstrapApplication() {
angular.element(document).ready(function () {
angular.bootstrap(document, ['myModule']);
});
}
fetchData().then(bootstrapApplication);
}());
您可以将此块重构为属于常规 angular 服务,以便它可以测试。例如(我假设您的调用可以 运行 并行):
angular.module('myBootstrapModule', [])
.factory('bootstrap', function($q, $http) {
return function(moduleName) {
return $q.all([
$http.get('./app/config/app.config.json'),
$http.get('./app/config/routes.config.json')
])
.then(function(results) {
angular.module(moduleName)
.value('globalConfig', results[0].data)
.value('routesConfig', results[1].data);
})
.catch(function(res) {
// handle error
});
}
}
});
然后可以通过手动创建包含此模块的注入器来实现引导,而测试可以通过加载 myBootstrapModule
:
var injector = angular.injector(['ng', 'myBootstrapModule']);
var bootstrap = injector.get('bootstrap');
bootstrap('myModule').then(function() {
// bootstrap angular
});