摩卡作为图书馆 [英] mocha as a library

查看:196
本文介绍了摩卡作为图书馆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用mocha(node.js测试框架,而不是ruby mocking库)作为库,而不是使用mocha可执行文件来运行我的测试。

I would like to use mocha (the node.js test framework, not the ruby mocking library) as a library, rather than using the mocha executable to run my test.

这样可以运行摩卡测试吗?示例都只是调用mocha库,假设它们已经是require'd,mocha可执行文件提前完成所有的require-ing,但是我真的更喜欢在我的脚本中明确的做,所以我可以简单地

Is it possible to run a mocha test this way? The examples all just call mocha libraries assuming they are already "require'd", and the mocha executable does all the "require-ing" ahead of time, but I would really prefer to do them explicitly in my script so that I can simply set +x on my script and call it directly.

我可以这样做吗?

#!/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()


推荐答案

此功能已添加。

我从此处:

您将需要2个文件。一个测试,一个运行测试。您可以将runTest标记为可执行文件,并在mocha选项中设置其输出。

You will need 2 files. One test, and one to run the test. You can mark runTest as executable, and set its output in the mocha options.

#!/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



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

这篇关于摩卡作为图书馆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆