Karma 没有 运行 任何测试(0 个错误,共 0 个错误)

Karma doesn't run any test (0 of 0 Errors)

我对自动化测试完全陌生,所以我从 angular 和 Karma+Jasmine 开始。

这是我的 karma.conf.js 文件

// test/spec/components/lib/search.js

// Karma configuration
// http://karma-runner.github.io/0.12/config/configuration-file.html
// Generated on 2015-10-20 using
// generator-karma 1.0.0

module.exports = function(config) {
  'use strict';

  config.set({
    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,

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

    // testing framework to use (jasmine/mocha/qunit/...)
    // as well as any additional frameworks (requirejs/chai/sinon/...)
    frameworks: [
      "jasmine"
    ],

    // list of files / patterns to load in the browser
    files: [
      // bower:js
      'bower_components/jquery/dist/jquery.js',
      'bower_components/angular/angular.js',
      'bower_components/angular-resource/angular-resource.js',
      'bower_components/angular-route/angular-route.js',
      'bower_components/angular-sanitize/angular-sanitize.js',
      'bower_components/angular-touch/angular-touch.js',
      'bower_components/angular-ui-router/release/angular-ui-router.js',
      'bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
      'bower_components/ngInfiniteScroll/build/ng-infinite-scroll.js',
      'bower_components/Chart.js/Chart.js',
      'bower_components/angular-chart.js/dist/angular-chart.js',
      'bower_components/bootstrap-sass/assets/javascripts/bootstrap.js',
      'bower_components/angular-mocks/angular-mocks.js',
      // endbower
    //      "app/components/lib/search.js",
    //      "test/mock/**/*.js",
      "test/spec/**/*.js"
    ],

    // list of files / patterns to exclude
    exclude: [
    ],

    // web server port
    port: 8080,

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

    // Which plugins to enable
    plugins: [
      "karma-phantomjs-launcher",
      "karma-jasmine"
    ],

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

    colors: true,

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

    // Uncomment the following lines if you are using grunt's server to run the tests
    // proxies: {
    //   '/': 'http://localhost:9000/'
    // },
    // URL root prevent conflicts with the site root
    // urlRoot: '_karma_'
   });
   };

这是我的单元测试

console.log(1);
describe('test sul modulo search', function() {
    console.log(2);
    beforeEach(module('search'));

    describe('searchObject', function() {
        console.log(3);
        var SearchObject;

        beforeEach(inject(function(_SearchObject_){
            console.log(4);
            // The injector unwraps the underscores (_) from around the parameter names when matching
            SearchObject = _SearchObject_;

            //it(...)
        }));
    });

});

基本上我是在检查服务给定对象的结构。 我 运行 grunt test 一切似乎都很好,直到这个

    Running "karma:unit" (karma) task
10 02 2016 17:14:25.645:INFO [karma]: Karma v0.13.19 server started at http://lo
calhost:8080/
10 02 2016 17:14:25.672:INFO [launcher]: Starting browser PhantomJS
10 02 2016 17:14:28.578:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on s
ocket /#x_Xg4CHTyVCYljSGAAAA with id 76913427
PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 1

PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 2

PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 3

PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 0 of 0 ERROR (0.006 secs / 0 secs)
Warning: Task "karma:unit" failed. Use --force to continue.

听起来好像没看到我的测试

编辑。我重新编写了我的测试如下(console.logs 添加):我只看到前 3

edit 2: it 块丢失了,有了它一切正常。 Howerev 现在测试失败,因为对象未定义。这是新的测试代码:

describe('test sul modulo search', function() {
    console.log(2);
    beforeEach(module('search'));

    describe('SearchObject test', function() {
        console.log(3);
        var SearchObject;

        beforeEach(inject(function(_SearchObject_){
            console.log(4);
            // The injector unwraps the underscores (_) from around the parameter names when matching
            SearchObject = _SearchObject_;
        }));

        describe('is defined', function() {
            it('should evaluate the injected SearchObject', function (){
                console.log(5);
                expect(SearchObject).toBeDefined()
            });
        });
    });

});

这是 jasmine

的输出
Running "karma:unit" (karma) task
11 02 2016 09:22:29.063:INFO [karma]: Karma v0.13.19 server started at http://lo
calhost:8080/
11 02 2016 09:22:29.094:INFO [launcher]: Starting browser PhantomJS
11 02 2016 09:22:31.833:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on s
ocket /#XlTyAkmOHjoihkAyAAAA with id 2744252
PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 1

PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 2

PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 3

LOG: 5
PhantomJS 2.1.1 (Windows 8 0.0.0) test sul modulo search SearchObject test is de
fined should evaluate the injected SearchObject FAILED
        C:/Users/Rick/Sviluppo/socialsider-fe/bower_components/angular/angular.j
s:4459:53
        forEach@C:/Users/Rick/Sviluppo/socialsider-fe/bower_components/angular/a
ngular.js:340:24
        loadModules@C:/Users/Rick/Sviluppo/socialsider-fe/bower_components/angul
ar/angular.js:4419:12
        createInjector@C:/Users/Rick/Sviluppo/socialsider-fe/bower_components/an
gular/angular.js:4344:22
        workFn@C:/Users/Rick/Sviluppo/socialsider-fe/bower_components/angular-mo
cks/angular-mocks.js:2428:60
        Expected undefined to be defined.
        C:/Users/Rick/Sviluppo/socialsider-fe/test/spec/search.js:19:49
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs / 0.04 sec
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.032 secs
/ 0.04 secs)

如您所见,缺少 log(4),这与 SearchObject 赋值块有关。是这个问题吗?

Jasmine 不会执行 beforeEach,因为您的套件中没有任何测试(它调用)

尝试以下方法

hereconsole.log(1);
describe('test sul modulo search', function() {
  console.log(2);

  var SearchObject;
  beforeEach(module('search'));

  beforeEach(inject(function(_SearchObject_){
        console.log(4);
        // The injector unwraps the underscores (_) from around the parameter names when matching
        SearchObject = _SearchObject_;
  }));

  describe('searchObject', function() {

        it('should evaluate the injected SearchObject', function (){
            console.log(3);
            expect(SearchObject).toBeDefined()
         });
  });
})