简体   繁体   English

注入常​​量时,未知提供程序的错误抛出

[英]Unknown provider's error throw when injecting constant

I am having some issues running my karma tests. 我在运行业力考试时遇到了一些问题。

I have this service that works well, in which I inject a constant CSRF_TOKEN : 我有这个服务运行良好,我注入一个常量CSRF_TOKEN

'use strict';

angular.module('App').factory("AuthenticationService", function($http,    $sanitize, SessionService, FlashService, CSRF_TOKEN) {

   var sanitizeCredentials = function(credentials) {
       return {
           email: $sanitize(credentials.email),
           password: $sanitize(credentials.password),
           csrf_token: CSRF_TOKEN
       };
   };
...

But when running grunt test command, Karma's error is : 但是当运行grunt test命令时,Karma的错误是:

Error: [$injector:unpr] Unknown provider: CSRF_TOKENProvider <- CSRF_TOKEN <- AuthenticationService

UPDATE My Karma.conf : 更新我的Karma.conf:

// Karma configuration
// http://karma-runner.github.io/0.10/config/configuration-file.html

module.exports = function(config) {
  config.set({
    // base path, that will be used to resolve files and exclude
    basePath: '',

    // testing framework to use (jasmine/mocha/qunit/...)
    frameworks: ['jasmine'],

    // list of files / patterns to load in the browser
    files: [
      'app/bower_components/angular/angular.js',
      'app/bower_components/jquery/dist/jquery.js',
      'app/bower_components/angular-mocks/angular-mocks.js',
      'app/bower_components/angular-resource/angular-resource.js',
      'app/bower_components/angular-cookies/angular-cookies.js',
      'app/bower_components/angular-sanitize/angular-sanitize.js',
      'app/bower_components/angular-route/angular-route.js',
      'app/bower_components/xdomain/dist/0.6/xdomain.js',
      'app/bower_components/sass-bootstrap/dist/js/bootstrap.js',
      'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
      'app/scripts/*.js',
      'app/scripts/**/*.js',
      'test/mock/**/*.js',
      'test/spec/**/*.js'
    ],

    // list of files / patterns to exclude
    exclude: [],

    // web server port
    port: 8080,

    // level of logging
    // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: false,


    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false
  });
};

And my constant defined in a bootstrap.js file to instantiate it before app starts : 我的常量在bootstrap.js文件中定义,以在应用程序启动之前实例化它:

'use strict';

angular.element(document).ready(function(){
    var app = angular.module('App');
    var $injector = angular.injector(['ng']);
    $injector.invoke(function($http, $rootScope) {
        $rootScope.$apply(function() {
            $http.get("http://something/token").then(function(response) {
                app.constant("CSRF_TOKEN", response.data.csrf_token);
                angular.bootstrap(document, ['App']);
            });
        });
    });
});

Do you know the solution so that Karma stop thinking my constant is a provider? 你知道解决方案让Karma不再认为我的常数是提供者吗?

It seams like document's ready event is never thrown in your jasmine test and therefore constant is not defined in your App module. 它的接缝就像文件的ready事件永远不会在你的茉莉花测试中抛出,因此你的App模块中没有定义常量。 Try to mock CSRF_TOKEN constant in your module configuration section of the test: 尝试在测试的模块配置部分中模拟CSRF_TOKEN常量:

JavaScript JavaScript的

describe('Test', function() {

  beforeEach(function() {
    module('App', function($provide) {
      $provide.constant('CSRF_TOKEN', 'MOCK_CONSTANT'); // <= mock your constant
    });
  });

  // Tests go here

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 注入工厂时出现未知的提供程序错误 - Unknown provider error when injecting factory AngularJS Getting Unknown提供程序错误,将常量注入工厂 - AngularJS Getting Unknown provider error injecting a constant into a factory 将AngularJS工厂注入控制器时出现未知的提供程序错误 - Unknown provider error when injecting AngularJS factory into controller 将服务注入控制器时,Angular获取未知的提供程序错误 - Angular getting unknown provider error when injecting service into controller 将多个服务模块注入控制器时,AngularJS未知提供程序错误 - AngularJS Unknown Provider Error When Injecting a Multiple Services Module into Controller 将服务注入Angular单元测试时出现Unknown Provider错误 - Getting Unknown Provider error when injecting a Service into an Angular unit test 将自定义过滤器注入服务时的“未知提供者” - “Unknown Provider” when injecting custom filter into service 从外部模块将提供程序注入配置块时发生未知的提供程序错误 - Unknown Provider error when injecting provider from a foreign module into config block 未知的提供者注入工厂 - Unknown provider injecting factory 用路由器进行AuglarFire身份验证会抛出未知的提供程序错误 - AuglarFire Authenticating With Routers throw unknown provider error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM