当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《注入常​​量时,未知提供程序的错误抛出》经验,为你挑选了1个好方法。

我在运行业力考试时遇到了一些问题.

我有这个服务运作良好,我注入一个常量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不再认为我的常数是提供者吗?



1> Vadim..:

它的接缝就像文件的ready事件永远不会在你的茉莉花测试中抛出,因此你的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

});

推荐阅读
大大炮
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有