【问题标题】:Unknown provider's error throw when injecting constant注入常​​量时引发未知提供程序的错误
【发布时间】:2014-04-14 09:05:21
【问题描述】:

我在运行我的业力测试时遇到了一些问题。

我有这个运行良好的服务,我在其中注入了一个常量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
       };
   };
...

但是在运行grunt test 命令时,Karma 的错误是:

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

更新 我的 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
  });
};

我的常量在 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']);
            });
        });
    });
});

您是否知道解决方案,以便 Karma 不再认为我的常量是提供者?

【问题讨论】:

  • 你能分享一下常量和因果报应吗?
  • 我更新了我的问题:)
  • 看起来只有在有 http 调用时才会设置常量:$http.get("http://something/token")。你如何在业力测试中做到这一点?
  • 顺便说一句,这看起来不像是一个常量。也许,您的设计需要令牌服务?
  • 我只是调用一次 url 来为我的 API 提供一个唯一的令牌,所以我认为我不需要服务。我尝试在我的 app.js 中实例化一个简单的常量,例如 angular.module('accredFrontApp').constant('CSRF_TOKEN','myconst');,但错误仍然相同。

标签: angularjs gruntjs jasmine karma-runner


【解决方案1】:

看起来像文档的 ready 事件永远不会在您的 jasmine 测试中抛出,因此您的 App 模块中没有定义常量。尝试在测试的模块配置部分模拟 CSRF_TOKEN 常量:

JavaScript

describe('Test', function() {

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

  // Tests go here

});

【讨论】:

  • 这不会改变错误:/我之前尝试过用angular.module('App').constant('CSRF_TOKEN','myconst'); 正常实例化一个常量,但我在运行测试时遇到了同样的错误......
  • @lightalex 问题不在于你如何定义一个常量,而在于你什么时候做。您确定angular.element(document).ready() 中的代码正在运行吗?您是否尝试按照我的示例修改测试?
  • 我看不到解决方案。模拟不是解决方案。有人能指点我吗?
猜你喜欢
  • 2014-07-08
  • 2014-12-15
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
  • 2015-03-28
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多