简体   繁体   English

使用karma-browserify对AngularJS应用程序进行单元测试

[英]Unit testing AngularJS application with karma-browserify

I'm trying to unit test an AngularJS/Browserify application via karma-browserify. 我正在尝试通过karma-browserify对AngularJS / Browserify应用程序进行单元测试。 Ultimately, when I run my gulp karma task, I get the error Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it... 最终,当我运行gulp karma任务时,出现Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it... Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it...

My gulpfile.js has the task 我的gulpfile.js有任务

gulp.task('test', function(done) {
    new karma.Server({
        configFile: __dirname + '/karma.conf.js'
    }, done).start();
});

My karma.conf.js includes 我的karma.conf.js包括

{
  // ...
  frameworks: ['browserify', 'jasmine'],
  files: [
    'node_modules/angular/angular.js',
    'node_modules/angular-mocks/angular-mocks.js',
    'spec/**/*.js'
  ],
  preprocessors: {
    'spec/**/*.js': [ 'browserify' ]
  },
  browserify: {
    debug: true
  }
  // ...
}

I define my module in a main.js that includes 我在main.js中定义了我的模块,其中包括

require('angular').module('myApp', [
  //...lots of `require`s for each dependency...
]);

I define my controller in a MainCtrl.js that looks like 我在MainCtrl.js中定义控制器,如下所示:

function MainCtrl(/*...deps...*/) {
  var ctrl = this;
  ctrl.foo = 'bar';
}

module.exports = MainCtrl;

then register the controller elsewhere like 然后在其他地方注册控制器

var app = require('angular').module('myApp');
app.controller('MainCtrl', [/*...deps...*/, require('./MainCtrl')]);

Finally my test looks like 最终我的测试看起来像

(function() {
    "use strict";

    describe('MainCtrl', function() {
        var ctrl;

        beforeEach( angular.mock.module('myApp') );

        beforeEach( inject( function($controller) {
            ctrl = $controller('MainCtrl');
        }));

        it('should be defined', function() {
            expect(ctrl).toBeDefined();
        });
    });
}());

Workaround 解决方法

The workaround I have is to add my main.js file to karma.conf.js 我要解决的方法是将main.js文件添加到karma.conf.js中

files: [
  'js/main.js', // ADDED: Defines the app and `require`s angular
  'node_modules/angular-mocks/angular-mocks.js',
  'spec/**/*.js'
],
preprocessors: {
  'js/main.js': ['browserify'], // ADDED
  'spec/**/*.js': ['browserify']
}

and everything works. 一切正常。 But I thought I wasn't supposed to add my source files to karma (at least not with karma-browserify). 但是我以为我不应该将我的源文件添加到业力中(至少不使用业力浏览器来添加)。 Is this the right way to set up my project? 这是设置我的项目的正确方法吗?

Yes, the 'workaround' is the desired way to use karma-browserify . 是的,“解决方法”是使用karma-browserify的理想方式。

preprocessors definition specifies which of the included files should be processed by which preprocessor but does not include them: preprocessors定义指定哪个preprocessors应处理哪些包含文件,但不包括它们:

The keys of the preprocessors config object are used to filter the files specified in the files configuration. 预处理程序配置对象的键用于过滤文件配置中指定的文件。

it is files definition that actually includes the files: files定义实际上包括文件:

The files array determines which files are included in the browser and which files are watched and served by Karma. files数组确定哪些文件包含在浏览器中以及哪些文件由Karma监视和提供。

Files tells Karma which files it should load relative to the base path. 文件告诉Karma应相对于基本路径加载哪些文件。 These are: 这些是:

All test related libraries Our source code to test The tests themselves 所有与测试相关的库我们要测试的源代码

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM