如何使空的占位符testing在摩卡故意失败?

我在NodeJS中编写一个API,并使用Mocha,Chai和SuperTest进行testing。 我正在使用一种典型的testing驱动方法来编写testing,然后使用工作代码来满足这些testing。 然而,由于所有不同排列的testing数量,我已经开始写空的占位符testing,以便我有所有的it('should...')描述来提醒我当我到达时要testing什么该function。 例如:

 it 'should not retrieve documents without an authorized user', (done) -> done() 

这个问题是done()被调用,没有任何断言,所以testing被认为是传递,所以我已经添加了下面的断言。

 false.should.equal true # force failure 

但这是一个破解和摩卡显示失败的原因可能看起来很混乱,尤其是当其他完整的testing可能失败。

是否有任何官方的方式故意失败在摩卡这样的占位符testing?

将testing标记为尚未准备好进行testing的正式方法是使用skip ,这是一种出现在describe和字段中的方法。 这是一个例子:

 describe("not skipped", function () { it("bar", function () { throw new Error("fail"); }); it.skip("blah", function () { throw new Error("fail"); }); }); describe.skip("skipped", function () { it("something", function () { throw new Error("fail"); }); }); 

上面的代码放在一个test.js文件中,运行$ mocha --reporter=spec test.js ,产生:

  not skipped 1) bar - blah skipped - something 0 passing (4ms) 2 pending 1 failing 1) not skipped bar: Error: fail at Context.<anonymous> (/tmp/t33/test.js:3:15) at callFn (/home/ldd/local/lib/node_modules/mocha/lib/runnable.js:223:21) at Test.Runnable.run (/home/ldd/local/lib/node_modules/mocha/lib/runnable.js:216:7) at Runner.runTest (/home/ldd/local/lib/node_modules/mocha/lib/runner.js:374:10) at /home/ldd/local/lib/node_modules/mocha/lib/runner.js:452:12 at next (/home/ldd/local/lib/node_modules/mocha/lib/runner.js:299:14) at /home/ldd/local/lib/node_modules/mocha/lib/runner.js:309:7 at next (/home/ldd/local/lib/node_modules/mocha/lib/runner.js:247:23) at Object._onImmediate (/home/ldd/local/lib/node_modules/mocha/lib/runner.js:276:5) at processImmediate [as _immediateCallback] (timers.js:354:15) 

前面的testing名称被跳过。 另外,在支持颜色的terminal中,跳过的testing显示为蓝色(对于失败的testing,反对为红色,对于传递,为绿色)。 据说一个跳过的testing是“等待中”的,所以Mocha将跳过testing的次数报告为“2未决”。

未执行的testing不应该fail ,它应该被标记为pending

将摩卡testing标记为not yet implemented的简洁方法是不将callback函数传递给it处理程序。

 describe("Traverse", function(){ describe("calls the visitor function", function(){ it("at every element inside an array") it("at every level of a tree") }) }) 

运行mocha test将显示您的未执行testing为待处理。

 $ mocha test Traverse calls the visitor function - at every element inside an array - at every level of a tree 0 passing (13ms) 2 pending 

如果你传递一个string或错误done()它会报告为错误。 所以:

 it 'should not retrieve documents without an authorized user', (done) -> done('not implemented') 

会导致testing失败,输出:

完成()调用与非错误:未实现


我喜欢@ Canyon的解决scheme,只是没有通过callback标记testing“挂起”,但在我的情况下,我希望这些占位符失败我的CI构build,所以使他们这样的实际失败testing更容易。

这实际上是一个很好的问题,因为我也会发现这个超级有用。 看完之后,我会想创build自己的wrapper“todo”或“fail”函数,以便在代码库中重用。

下面的例子使用了一个名为todo的函数,它将打印出“尚未实现”的文本。 这可能作为一个单独的模块是有用的,即使你可以导入和使用所有的testing。 可能需要修改一下代码

柴的例子断言

 var assert = require('chai').assert; function todo() { assert(false, "method not yet implemented"); }; describe("some code", function(){ it("should fail something", function(){ todo(); }); }); 

使用chai assert和失败选项的例子(虽然看起来没有用)

 var assert = require('chai').assert; function todo() { assert.fail(true, true, "method not yet implemented"); //1st 2 args can be anything really }; describe("some code", function(){ it("should fail something", function(){ todo(); }); });