摩卡作为图书馆
mocha as a library
我想使用mocha(node.js测试框架,而不是ruby mocking库)作为库,而不是使用mocha可执行文件来运行我的测试。
有可能用这种方式进行摩卡测试吗?这些例子都只是调用mocha库,假设它们已经是"required",而mocha可执行文件提前完成了所有的"requireing",但我真的更喜欢在我的脚本中显式地执行它们,这样我就可以简单地在脚本上设置+x并直接调用它。
我能做这样的事吗?
#!/usr/bin/env coffee
mocha = require 'mocha'
test = mocha.Test
suite = mocha.Suite
assert = require("chai").assert
thing = null
suite "Logging", () ->
setup (done) ->
thing = new Thing()
done()
test "the thing does a thing.", (done) ->
thing.doThing () ->
assert.equal thing.numThingsDone, 1
done()
teardown (done) ->
thing = null
done()
这是可能的,但肯定不推荐。
查看mocha二进制文件的源代码(特别是bin/_mocha
),了解它的作用。特别是run
函数。它使用的所有东西——Runner
、Reporter
等等——都是由mocha库导出的,所以没有什么可以阻止你对它进行逆向工程
此功能已添加。我在下面举了一个例子。
我从这里得到信息:
您将需要2个文件。一个测试,一个运行测试。您可以将runTest标记为可执行文件,并在mocha选项中设置其输出。
runTest.js
#!/usr/bin/env node
var Mocha = require('mocha'),
fs = require('fs'),
path = require('path');
var mocha = new Mocha(
{
ui: 'tdd'
});
mocha.addFile(
path.join(__dirname, 'test.js')
);
mocha.run(function(failures){
process.on('exit', function () {
process.exit(failures);
});
});
test.js
var assert = require('chai').assert
suite('Array', function(){
setup(function(){});
suite('#indexOf()', function(){
test('should return -1 when not present', function(){
assert.equal(-1, [1,2,3].indexOf(4));
});
});
});
下面的代码片段允许您以编程方式控制Node之外的Mocha主要功能,如添加套件和在不同步骤中运行套件。关键的一点是找出如何使mocha接口全局可访问(该代码也可作为要点)
var Mocha = require("mocha");
var mocha = new Mocha();
var _suites = [];
var _done = false;
/**
* default mocha options
* for other configurations, check out bin/_mocha
*/
mocha.reporter("nyan");
mocha.useColors(true);
mocha.growl();
module.exports = {
/**
* set interface (bdd is default) and make it global to node
* @param {string} interface bdd|tdd|exports|qunit
*/
init: function(interface) {
interface && mocha.ui(interface);
mocha.suite.emit('pre-require', global, undefined, mocha);
},
/**
* add suite
* @param {function} suite to be executed later
*/
add: function(suite) {
mocha.suite && _suites.push(suite) && suite();
},
/**
* run added suites
*/
run: function(cb) {
console.info('run mocha');
var done = function () {
_done = true;
cb && cb();
process.on('exit', function() {
console.info("exit mocha");
process.exit(1);
});
};
mocha.run(done);
},
/**
* end mocha test
*/
exit: function() {
if (_done) {
process.exit();
} else {
var start = new Date().getTime(),
interval = setInterval(function() {
if (_done) {
console.log("test suites finished");
clearInterval(interval);
process.exit();
} else if (new Date().getTime() - start > 5000) {
console.log("wait for nothing!");
clearInterval(interval);
process.exit();
}
}, 250);
}
},
/**
* change mocha options at runtime, e.g., "reporter", "nyan"
*/
_set: function(key, val){
mocha[property](val);
}
};
相关文章:
- 摩卡由于绝对路径而不断轰炸
- 如何使用浏览器中用酶创建的组件反应例如摩卡
- 找不到模块'它'当用摩卡/柴测试时
- 摩卡+柴+jsdom Can't加载自定义模块目录
- 如何将 XML 文件包含在摩卡测试用例中
- 摩卡与敏捷(异步)冲突
- 用于摩卡测试的 CLI 调试器
- 如何在摩卡测试中使用“定义”
- 摩卡“每个钩子之前”红色消息.我怎么知道具体出了什么问题
- 错误:使用摩卡测试时找不到模块
- 大口喝摩卡如何传递编译器标志
- 我可以从摩卡测试MarkLogic SJS吗
- 用摩卡测试失败的承诺'内置的promise支持
- 如何用摩卡/柴制作模拟窗口/文档
- 使用带量角器的摩卡无法通过超时测试
- 未定义的Es6级在单元测试中与大口摩卡
- 摩卡和西农方法间谍
- 从命令行一次运行大量摩卡测试的好方法是什么
- 如何在多个文件中设置摩卡测试用例的执行顺序
- 摩卡作为图书馆