4

我有一个问题,我试图让 Karma 跑步者执行我使用 RequireJS 加载的 mocha 规范。不幸的是,我无法弄清楚为什么即使框架正在加载,规范也不会执行。以下是我希望的相关位:

// karma.conf.js
// Karma configuration
// Generated on Thu Jun 13 2013 13:38:06 GMT-0500 (CDT)


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


// list of files / patterns to load in the browser
files = [
MOCHA,
MOCHA_ADAPTER,
REQUIRE,
REQUIRE_ADAPTER,

// !! libs required for test framework
{pattern: 'test/lib/chai.js', included: false},

// !! put what used to be in your requirejs 'shim' config here
'app/bower_components/angular/angular.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-sanitize/angular-sanitize.js',
'app/bower_components/angular-scenario/angular-scenario.js',
'app/bower_components/jquery/jquery.js',


{pattern: 'app/scripts/**/*.js', included: false},
{pattern: 'test/**/*Spec.js', included: false},

'test/test-main.js'
];


// list of files to exclude
exclude = [
  'app/scripts/main.js'
];


// 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_INFO;


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


browsers = ['Chrome'];


 // 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;

然后这是我处理 requireJS 加载的 test-main.js 文件

var tests = [];
for (var file in window.__karma__.files) {
    if (/Spec\.js$/.test(file)) {
        tests.push('../../' + file.replace(/^\/base\//, '').replace(/\.js$/, ''));
    }
}
requirejs.config({
  baseUrl: '/base/app/scripts/',
  paths: {
    chai: "../../test/lib/chai",
    namespace: "vendor/namespace",
    jquery: "../bower_components/jquery/jquery",
    bootstrap: "vendor/bootstrap",
    angular: "../bower_components/angular/angular",
    angularCookies: "../bower_components/angular-cookies/angular-cookies",
    angularResource: "../bower_components/angular-resource/angular-resource",
    angularSanitize: "../bower_components/angular-sanitize/angular-sanitize",
    applicationController: "controllers/application",
    gameController: "controllers/game",
    gamePresenter: "directives/game-presenter",
  }
});

require(tests, function(){
  window.__karma__.start();
});

这是我正在运行的规范示例:

define(['chai', 'namespace','racecar'],
  function(chai, namespace, racecar) {
    var assert = chai.assert,
    expect = chai.expect,
    should = chai.should();

    // This executes correctly!
    var player = new com.angular.racecar.Player();
    player.should.be.an('object');

  // This never gets run!
  describe('Player', function () {
    it('should be an object', function () {
      var player = new com.angular.racecar.Player();
      player.should.be.an('object');
    });
  });
});

这是我正在测试的代码示例:

(function() {
  "use strict";
  var Player;

  namespace('com.angular.racecar', {
    Player: Player = (function() {
      function Player() {
        this.car = new com.angular.racecar.Car();
        return this;
      }
      return Player;
    })()
   });
}(this)

输出简单地说:

INFO [Chrome 27.0 (Mac)]: Connected on socket id fc4Kj9T0ppIzp9D0kmdH
Chrome 27.0 (Mac): Executed 0 of 0 SUCCESS (0.192 secs / 0 secs)
4

2 回答 2

0

要使用 mocha 测试,您需要使用 Angular JS 的不稳定分支,其中 >= v1.1.1

您可以在这里看到他们添加了 mocha 支持:https ://github.com/angular/angular.js/blob/master/CHANGELOG.md#111-pathological-kerning-2012-11-26

差异:https ://github.com/angular/angular.js/commit/92558fe4119fb1ee793d781de1888abef181c7f6

于 2013-06-29T23:53:00.097 回答
0

Angular-mock 应该只适用于 Jasmine。要使其与 Mocha 一起使用,您需要使用他们的测试文章中描述的http://www.yearofmoo.com/创建的修改后的 angular-mock。该文件的直接链接是https://github.com/yearofmoo-articles/AngularJS-Testing-Article/tree/master/test/lib/angular

于 2013-06-18T15:04:49.317 回答