0
我有很多麻煩,爲包含一些注射的提供者編寫單元測試用例。AngularJS - 供應商和注射的Karma測試
的特定供應商是:
(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);
});
});
});
我不斷收到這樣的錯誤:
- Error: [$injector:unpr] Unknown Provider: $stateProvider
- Error: [$injector:unpr] Unknown Provider: $urlRouterProvider
- Error: [$injector:unpr] Unknown Provider: routerHelperProvider
我嘗試了幾種不同的模塊實例和幾個不同的注射,但我似乎無法使其工作。當我取出注射劑($stateProvider
,$urlRouterProvider
和$state
)時,單元測試很簡單。