Angularjs Can';“我不能动脑”;喷油器已创建,无法注册模块&引用;错误

Angularjs Can';“我不能动脑”;喷油器已创建,无法注册模块&引用;错误,angularjs,Angularjs,我的应用程序似乎运行良好。我开始编写测试用例,并且已经创建了可怕的注入器,无法注册模块错误 这是我的测试代码。这些是文件中唯一的行 'use strict'; var fac, osf, obff; beforeEach(module("myApp")); beforeEach(inject(function (OrderSashingFactory) { fac = OrderSashingFactory; })); 我真的不知道该从这里走到哪里——我在兜圈子

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

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

'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
    });
};

您没有将测试包装在一个
描述
,它看起来像这样:

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

    var fac,
        osf,
        obff;

    beforeEach(module("myApp"));

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

如果混合调用
module('someApp')
inject($someDependency)
,则会出现此错误


所有对
module('someApp')
的调用必须在调用
inject($someDependency)
之前进行。要添加@axelduch的回答,我遇到了另一个文件中没有描述的问题,这就是导致我的Injector已经创建,无法注册模块的原因

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

然后确保在“此处描述”中运行的每个测试都包含自己的描述方法。例如,Test1文件如下所示

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

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

这也可能意味着你有如下情况:

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
});

确保按正确的顺序进行测试。

如果有karma.conf.js,可以给我们看一下吗?您还没有在
description('Foo',/*您的函数和给定的代码*/)中包装测试可能是问题吗?那就是问题所在。我花了几个小时看我的注射器:-/@aduch你能把你的评论作为一个答案,让它更加突出吗?你的评论对我来说也是正确的解决方案。@AtesGoral你说得对:)你能给我们举个例子吗?
module(function($provide) {
   // Provide code
})

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