Karma没有执行任何单元测试

Dar*_*ren 25 unit-testing jasmine angularjs karma-runner

我似乎在让我的Jasmine单元测试实际执行时遇到问题.我已经通过将logLevel设置为LOG_DEBUG来验证我的所有脚本都已加载.我的单元测试与服务测试@ https://github.com/angular/angular-seed/blob/master/test/unit/servicesSpec.js相同.

此外,我使用了Testacular(在它重命名为Karma之前),我不记得有这个问题.我似乎让Chrome运行,但我必须手动点击"调试"按钮.即使我点击此按钮,我的测试也不会运行.

系统细节:

  • Windows 7的
  • 节点v0.10.4
  • Chrome 26.0.14
  • Karma 0.8.5(安装了3个警告 - 2个精度损失和1个'没有定义内联函数v8 :: Persistent v8 :: Persistent :: New(v8 :: Handle)')

这是我的模块代码(Scripts/main.js):

angular.module('sb.services', []).value('version', '0.0.1').value('amplify', amplify);
angular.module('sb.directives', []);
angular.module('sb.filters', []);
angular.module('sb.controllers', []).controller('SbController', [
    '$scope', 
    'amplify', 
    function ($scope, amplify) {
        $scope.message = 'Hello World! (amplify exists?=' + !!amplify + ')';
    }
]);
angular.module('sb', [
    'sb.services',
    'sb.directives',
    'sb.filters',
    'sb.controllers'
]);
Run Code Online (Sandbox Code Playgroud)

这是我的规范(Test/unit/servicesSpec.js):

'use strict';

describe('my services', function () {
    beforeEach(module('sb.services'));

    describe('version', function () {
        it('should return current version', inject(function(version) {
            expect(version).toEqual('0.0.1');
        }));
    });
});
Run Code Online (Sandbox Code Playgroud)

这是我的karma.conf.js文件:

// Karma configuration
// Generated on Mon Apr 15 2013 20:56:23 GMT-0400 (Eastern Daylight Time)


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


// list of files / patterns to load in the browser
files = [
  JASMINE,
  JASMINE_ADAPTER,
  'Vendor/angular-1.0.6/angular.js',
  'Vendor/angular-1.0.6/angular-*.js',
  'Vendor/json2/json2.js',
  'Vendor/jquery/jquery-1.8.2.js',
  'Vendor/amplify/amplify.js',
  'Scripts/main.js',
  'Test/unit/*.js'
];


// list of files to exclude
exclude = [

];


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


// web server port
port = 9876;


// cli runner port
runnerPort = 9100;


// enable / disable colors in the output (reporters and logs)
colors = true;


// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel = LOG_WARN;


// enable / disable watching file and executing tests whenever any file changes
autoWatch = false;


// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers = ['Chrome'];

junitReporter = {
    outputFile: 'Test/out/unit.xml',
    suite: 'unit'
};


// 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;
Run Code Online (Sandbox Code Playgroud)

Sly*_*nal 15

我刚刚遇到了同样的问题,我发现autoWatch = true在Karma自动运行测试之前我必须设置.


Dar*_*ren 14

或者,您可以在karma.conf.js中使用exclude部分

exclude = [
  'Vendor/angular-1.0.6/angular-scenario.js'
];
Run Code Online (Sandbox Code Playgroud)


Rya*_*inn 12

我的最后一个答案是错误的(将angular.js行下面的JASMINE和JASMINE_ADAPTER行移动).它解决了这个特殊问题,但却产生了其他问题.相反,我所做的是修复它只是指定angular-mocks文件,而不是angular-*,如下所示:

   JASMINE,
   JASMINE_ADAPTER,
  'Vendor/angular-1.0.6/angular.js',
  'Vendor/angular-1.0.6/angular-mocks.js',
  'Vendor/json2/json2.js',
  'Vendor/jquery/jquery-1.8.2.js',
  'Vendor/amplify/amplify.js',
  'Scripts/main.js',
  'Test/unit/*.js'
Run Code Online (Sandbox Code Playgroud)

  • 这是因为angular-scenario文件用于端到端测试.我认为它与angular-mocks文件之间存在冲突. (5认同)

小智 8

我没有成功地尝试了以上所有,直到最后...

在我的karma.conf.js我删除了requirejs依赖,如:

    frameworks: ['jasmine', 'requirejs'],
Run Code Online (Sandbox Code Playgroud)

至:

    frameworks: ['jasmine'],
Run Code Online (Sandbox Code Playgroud)


Sna*_*hot 7

如果您尝试使用JASMINE和JASMINE_ADAPTER解决此问题,则不再支持此问题(至少在Karma版本0.10.2上).

而是使用:

frameworks: ['jasmine']
Run Code Online (Sandbox Code Playgroud)

在您的Karma配置文件中.你可以在这里阅读这个.

我还发现在我的文件数组中我设置included: false了一个模式. included仅在您要手动包含Javascript文件时使用(例如,如果您将使用require.js).如果将从该模式加载所有测试,您将看到类似于以下内容的消息:

PhantomJS 1.9.2 (Linux): Executed 0 of 0 ERROR (0.151 secs / 0 secs)
Run Code Online (Sandbox Code Playgroud)

因为从不包含包含测试的文件.included: false从我的模式的文件数组中删除修复此问题,因为未指定默认值是true.