2017-04-25 108 views
0

我有一個噶摩卡測試注入的angularjs服務如何注入AngularJS服務卡瑪 - 摩卡測試

服務代碼:

(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(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

你能解釋一下你的代碼,例如更好你的代碼正在做什麼,你想用幾句話來達成什麼目標? – Felix

+0

你可以嘗試在'beforeEach(module('portal'));''後更換'angular.mock.module('portal',function(){});'' – sandyJoshi

+0

嗨@FelixHäberle,我有一個簡單的服務,返回一個格式化的金額。我試圖注入它在我的業力,摩卡測試,但我得到的錯誤:錯誤:[$注射器:unpr]未知的提供者:AmountFormatServiceProvider < - AmountFormatService –

回答

0

您需要包括portal模塊,而不是嘲笑它:

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