我有一个注入angularjs服务的Karma-Mocha测试
服务代码:
(function() {
'use strict';
angular.module('portal').service(
'AmountFormatService',
function() {
return {
format : function(amount) {
if (amount != null) {
var isNegative = false;
if (amount.substring(0, 1) == '('
|| amount.substring(0, 1) == '-'
|| amount.substring(1, 2) == '-'
|| amount.substring(1, 2) == '(') {
isNegative = true;
}
amount = amount.replace(/[^0-9.]/g, '');
if (isNegative) {
amount = '-' + amount;
}
}
if (amount == '')
amount = null;
return amount;
}
}
});
})();
Karma测试:
describe('AmountFormatService', function () {
var amountFormatService;
beforeEach(function(){
angular.mock.module('portal', function(){
});
inject(function(_AmountFormatService_){
amountFormatService = _AmountFormatService_;
});
});
it('does something', function(){
//Expect this to do something
console.log(amountFormatService.format(23));
});
});
karma.conf.js(karma配置文件):
module.exports = function (config) {config.set({
frameworks: ['mocha', 'chai'],
files: [
'../../../node_modules/angular/angular.js',
'../../../node_modules/angular-mocks/angular-mocks.js',
'../../main/webapp/app.js',
'../../main/webapp/services/*.js',
'../../test/testAmountFormatService2.specs.js',
'../../main/webapp/services/AmountFormatService.js'
],
// coverage reporter generates the coverage
reporters: ['progress', 'coverage'],
preprocessors: {
// source files, that you wanna generate coverage for
// do not include tests or libraries
// (these files will be instrumented by Istanbul)
// '../../test/*.js': ['coverage']
'../../main/webapp/services/*.js': ['coverage']
},
coverageReporter: {
dir : '../../../target/JSCodeCoverage/',
reporters: [
// reporters not supporting the `file` property
{ type: 'html', subdir: 'report-html' },
// reporters supporting the `file` property, use `subdir` to directly
// output them in the `dir` directory
{ type: 'cobertura', subdir: '.', file: 'cobertura.txt' }
]
},
browsers: ['Firefox', 'IE'],
autoWatch: true,
plugins: [
'karma-firefox-launcher',
'karma-ie-launcher',
'karma-junit-reporter',
'karma-mocha',
'karma-chai',
'karma-coverage'
]
})
}
问题是我每次运行测试时都会收到此错误:
Error: [$injector:unpr] Unknown provider: AmountFormatServiceProvider <- AmountFormatService
http://errors.angularjs.org/1.6.4/$injector/unpr?p0=AmountFormatServiceProvider%20%3C-%20AmountFormatService
at Anonymous function (E:/c014111/git/eliminator/eliminator-client/node_modules/angular/angular.js:4789:13)
at getService (E:/c014111/git/eliminator/eliminator-client/node_modules/angular/angular.js:4944:11)
at Anonymous function (E:/c014111/git/eliminator/eliminator-client/node_modules/angular/angular.js:4794:13)
at getService (E:/c014111/git/eliminator/eliminator-client/node_modules/angular/angular.js:4944:11)
at injectionArgs (E:/c014111/git/eliminator/eliminator-client/node_modules/angular/angular.js:4968:9)
at invoke (E:/c014111/git/eliminator/eliminator-client/node_modules/angular/angular.js:4995:7)
at WorkFn (E:/c014111/git/eliminator/eliminator-client/node_modules/angular-mocks/angular-mocks.js:3173:11)
at angular.mock.inject (E:/c014111/git/eliminator/eliminator-client/node_modules/angular-mocks/angular-mocks.js:3143:5)
at Anonymous function (E:/c014111/git/eliminator/eliminator-client/src/test/testAmountFormatService2.specs.js:9:9)
Error: Declaration Location
at angular.mock.inject (E:/c014111/git/eliminator/eliminator-client/node_modules/angular-mocks/angular-mocks.js:3140:9)
at Anonymous function (E:/c014111/git/eliminator/eliminator-client/src/test/testAmountFormatService2.specs.js:9:9)
任何人都可以帮助解决问题吗?
答案 0 :(得分:0)
您需要包含portal
模块,而不是模拟它:
- angular.mock.module('portal', function(){
- });
+ beforeEach(module('portal'));