设置Karma用jasmine,requirejs运行测试并做出反应

时间:2015-12-14 13:09:51

标签: javascript unit-testing requirejs karma-runner karma-jasmine

我正在创建自己的bootstrap project,其中包括RequireJS for modules structure,Backbone for routing and simple static views,ReactJS for components。该项目工作正常,但我想在未来的项目中添加单元测试支持以覆盖我的代码单元。我试图在单元测试中将Karma添加为Jasmine和RequireJS支持的测试运行器(然后我将添加TestUtils进行组件测试)。

目前我和卡玛有些麻烦......这对我不起作用...... 我用karma的配置创建了Gruntfile.js:

karma: {
    unit: {
        configFile: 'karma.config.js'
    }
}

我还添加了karma.config.js文件和配置。此外,我还为测试test-main.js

添加了带有RequireJS配置的文件

但是在尝试使用命令grunt test运行测试时,我遇到了麻烦:

grunt test

Running "karma:unit" (karma) task
14 12 2015 15:06:17.594:WARN [karma]: No captured browser, open http://localhost:9876/
14 12 2015 15:06:17.634:INFO [karma]: Karma v0.13.15 server started at http://localhost:9876/
14 12 2015 15:06:17.644:INFO [launcher]: Starting browser PhantomJS
14 12 2015 15:06:18.849:INFO [PhantomJS 1.9.8 (Windows 7 0.0.0)]: Connected on socket Gvi8xqI0lfbPEEOGAAAA with id 60807377
PhantomJS 1.9.8 (Windows 7 0.0.0): Executed 0 of 0 ERROR (0.013 secs / 0 secs)

在这个消息上,程序被卡住......有人有用的建议???

PS:我使用本教程将RequireJS与Karma测试一起使用:http://karma-runner.github.io/0.8/plus/RequireJS.html

1 个答案:

答案 0 :(得分:1)

添加到karma.config.js

files: [
    {pattern: 'bower_components/**/*.js', included: false},
    {pattern: 'scripts/**/*.js', included: false},
    {pattern: 'spec/**/*Spec.js', included: false},
    'spec/test-main.js'
],

exclude: [
    'scripts/main.js'
],

frameworks: ['jasmine', 'requirejs'],

plugins: ['karma-phantomjs-launcher', 'karma-requirejs', 'karma-jasmine'],

从命令行运行:

npm install requirejs --save-dev

spec/test-main.js

var pathToModule = function (path) {
    console.log(path);
    return path.replace(/^spec\//, '../spec/').replace(/\.(js)$/, '');
};

...
allTestFiles.push(pathToModule(normalizedTestModule));
...

baseUrl: '/base/scripts',

并删除

urlArgs: ...