Karma没有运行任何测试(0个错误中的0个)

时间:2016-02-10 15:27:57

标签: angularjs unit-testing phantomjs karma-jasmine

我对自动化测试非常陌生,所以我从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.

听起来没有看到我的测试

修改。我重新编写了我的测试作为follow(console.logs添加):我只看到前3个

编辑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分配区块有关。这是问题吗?

1 个答案:

答案 0 :(得分:2)

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