无法解决问题"注射器已经创建,无法注册模块!"错误

时间:2014-08-29 03:09:09

标签: angularjs

我的应用似乎运行良好。我开始编写测试用例并且已经创建了可怕的 Injector,无法注册模块!错误。

这是我的测试代码。这些是文件中的唯一行。

'use strict';

var fac,
    osf,
    obff;

beforeEach(module("myApp"));

beforeEach(inject(function (OrderSashingFactory) {
    fac = OrderSashingFactory;
}));

我真的不知道从哪里开始 - 我是在圈子里。

编辑 - 这是我的karma.conf.js文件。我还有其他测试运行良好。

// Karma configuration
// Generated on Mon Aug 25 2014 21:08:59 GMT-0400 (Eastern Daylight Time)

module.exports = function (config) {
    config.set({

        // base path, that will be used to resolve files and exclude
        basePath: '',


        // frameworks to use
        frameworks: ['mocha', 'chai', 'sinon'],


        // list of files / patterns to load in the browser
        files: [
            'app/bower_components/angular/angular.js',
            'app/bower_components/angular-route/angular-route.js',
            'app/bower_components/angular-mocks/angular-mocks.js',
            'app/js/*.js',
            'app/test/js/*.js',
            'app/partials/**/*.html'
        ],

        preprocessors: {
            'app/partials/**/*.html' : 'html2js'
        },

        ngHtml2JsPreprocessor: {
            // strip app from the file path
            stripPrefix: 'app/'
        },

        // list of files to exclude
        exclude: [

        ],

//        plugins: [
//            'karma-mocha',
//            'karma-chrome-launcher'
//        ],


        // test results reporter to use
        // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
        reporters: ['progress'],


        // web server port
        port: 9876,


        // enable / disable colors in the output (reporters and logs)
        colors: true,


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


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


        // Start these browsers, currently available:
        // - Chrome
        // - ChromeCanary
        // - Firefox
        // - Opera (has to be installed with `npm install karma-opera-launcher`)
        // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
        // - PhantomJS
        // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
        browsers: ['Chrome'],


        // If browser does not capture in given timeout [ms], kill it
        captureTimeout: 60000,


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

4 个答案:

答案 0 :(得分:24)

你还没有将你的考试包裹在describe中,它看起来像这样:

describe('MyTestName', function () { 
    'use strict';

    var fac,
        osf,
        obff;

    beforeEach(module("myApp"));

    beforeEach(inject(function (OrderSashingFactory) {
        fac = OrderSashingFactory;
    }));
});

答案 1 :(得分:5)

如果您要调用module('someApp')inject($someDependency),则会收到此错误。

在致电module('someApp')之前,您必须拨打inject($someDependency)的所有电话。

答案 2 :(得分:1)

要在@axelduch的答案之上添加,我遇到了另一个文件没有描述的问题,这就是导致我的 Injector已经创建的原因,无法注册模块!

如果您使用许多链接的文件运行测试(这是使用requirejs)

define(["require", "exports", "./test1.tests", "./test2.tests"], function (require, exports, Test1, Test2) {
    describe("Test Module", function () {
        Test1.tests();
        Test2.tests();
    });
});

然后确保在此描述中运行的每个测试在其中包含自己的描述方法。例如,Test1文件看起来像这样

    function tests() {
        describe("Test directive", function () {
            beforeEach(function () {
            });
            afterEach(function () {
            });
        });
    }

确保您的测试范围内的所有链接测试文件都有自己的描述方法,否则您可能不知道它,但这是另一项让您的设备测试失败的测试..它可能需要你很长时间才意识到这一点。

答案 3 :(得分:0)

这也意味着你有以下内容:

inject(function ($compile, $rootScope, $document, $timeout) {
   // Code processing inject
});

module(function($provide) {
   // Provide code
})

这是错误的,测试运行员将不允许您按此顺序执行此操作,因为它已经忙于注入并且您已经过了设置提供的阶段。

正确的方法当然是:

module(function($provide) {
   // Provide code
})

inject(function ($compile, $rootScope, $document, $timeout) {
   // Code processing inject
});

确保按正确顺序排列。