我在为包含一些注入的提供者编写单元测试用例时遇到了很多麻烦。
具体的提供者是:
(function () {
angular
.module('core.router', [])
.provider('routerHelper', routerHelperProvider);
routerHelperProvider.$inject = ['$stateProvider', '$urlRouterProvider'];
/**
* This method Initializes the Router Helper provider to be used by all modules
*
* @param $stateProvider
* @param $urlRouterProvider
*/
function routerHelperProvider($stateProvider, $urlRouterProvider) {
this.$get = routerHelperService;
routerHelperService.$inject = ['$state'];
/**
* This method sets the methods to be used by the helper
*
* @param $state
* @returns {Object}
*/
function routerHelperService($state) {
var hasOtherwise = false;
return {
configureStates: configureStates,
getStates: getStates
};
/**
* This method configures all the states to be used by the application
*
* @param {!String[]} states
* @param {!String} otherwisePath
*/
function configureStates(states, otherwisePath) {
states.forEach(function (state) {
//console.log("adding state", state.state, "with config", state.config);
$stateProvider.state(state.state, state.config);
});
if (otherwisePath && !hasOtherwise) {
hasOtherwise = true;
$urlRouterProvider.otherwise(otherwisePath);
}
}
/**
* This method returns the states to be used by the application
*
* @return {Object}
*/
function getStates() {
return $state.get();
}
}
} })();
基本的单元测试是:
'use strict';
describe('core.router test', function () {
// All Service injections
var $urlRouterProvider, $stateProvider;
// Mocks
var m_url = function () {
};
var m_state = function () {
};
// Others
var routerHelper, urlRouter, state, base;
// Before statements
beforeEach(module('core.router', function ($provide, _routerHelperProvider_) {
$provide.value('$urlRouterProvider', m_url);
$provide.value('$stateProvider', m_state);
base = _routerHelperProvider_;
}));
// Starting the Factory
beforeEach(inject(function (_routerHelper_, _$urlRouter_, _$state_) {
routerHelper = _routerHelper_;
urlRouter = _$urlRouter_;
state = _$state_;
}));
describe('when testing it', function () {
it('should return true', function () {
//var abc = routerHelper.getStates();
expect(1).toEqual(1);
});
});
});
我不断收到以下错误:
- 错误:[$injector:unpr] 未知提供者:$stateProvider
- 错误:[$injector:unpr] 未知提供者:$urlRouterProvider
- 错误:[$injector:unpr] 未知提供者:routerHelperProvider
我尝试了几种不同的模块实例化和几种不同的注入,但我似乎无法使其工作。当我取出注入($stateProvider
和$urlRouterProvider
)$state
时,单元测试很简单。