我有一个问题,我试图让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;
然后这是我的test-main.js文件,它处理requireJS加载
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)
答案 0 :(得分:0)
Angular-mock应该只适用于Jasmine。要使其与Mocha一起使用,您需要使用他们的测试文章中描述的http://www.yearofmoo.com/创建的修改角度模拟。该文件的直接链接是https://github.com/yearofmoo-articles/AngularJS-Testing-Article/tree/master/test/lib/angular
答案 1 :(得分:0)
要使用mocha测试,您需要使用angular JS的unstable分支,并且在> = 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