【问题标题】:How to Inject AngularJS service to karma-mocha test如何将 AngularJS 服务注入 karma-mocha 测试
【发布时间】:2017-04-25 11:52:06
【问题描述】:

我有一个注入 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;
                }
            }
        }); 
})();

业力测试:

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(业力配置文件):

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)

任何人都可以帮助解决似乎是什么问题?

【问题讨论】:

  • 你能更好地解释你的代码吗?你的代码在做什么,你想用几句话实现什么?
  • angular.mock.module('portal', function(){});换成beforeEach(module('portal'));后可以试试吗?
  • 嗨@FelixHäberle,我有一个简单的服务,它返回一个格式化的金额。我试图在我的 karma-mocha 测试中注入它,但我收到错误:错误:[$injector:unpr] Unknown provider: AmountFormatServiceProvider
  • 我试过你的建议@sandyJoshi,但我仍然遇到同样的错误。

标签: javascript angularjs mocha.js karma-runner karma-mocha


【解决方案1】:

您需要包含portal 模块,而不是模拟它:

- angular.mock.module('portal', function(){
- });
+ beforeEach(module('portal'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2021-07-23
    • 2013-04-07
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多