如何继续 运行 一个已经失败的 mocha `it`?

How to continue running a mocha `it` that has already failed?

以下mocha it语句:

it('should do truthy and falsey things', function() {
    var val = true;
    assert.equal(val, true, "true should be true!");
    console.log('state 1:', val);
    val != val;
    assert.equal(true, false, "should continue running even after failing");
    console.log('state 2:', val);
    val != val;
    assert.equal(false, false, "false should be false!");
});

结果如下:

state 1: true
    1) should do truthy and falsey things

就是这样。一旦第二个断言失败,函数的其余部分就不会 运行.

是否可以拥有函数的其余部分(在本例中为最后三行),如果可以,如何?

防止断言失败直接结束测试的最直接方法是捕获失败抛出的异常并稍后重新抛出。但是,我不建议这样做,因为这会造成并发症。如果不止一项测试失败,您要重新抛出哪个异常?另外,你必须记住重新抛出你捕捉到的异常,否则测试似乎会通过。

通常我只是接受这样一个事实:如果我有一个包含多个断言的测试,那么测试将在第一次失败时停止。在我无法忍受的情况下,我通常采用一种策略,将检查记录到一个结构中,然后在测试的最后将该结构与预期值进行比较。可以通过多种方式构建结构。这是一个例子:

it('should allow multiple tests', function () {
    var expected = {
        "something should be true": true,
        "something should be false": false,
        "forgot this one": 1
    };

    var actual = {};

    var thing_to_test = true;

    // First test
    actual["something should be true"] = thing_to_test;

    // Second test
    actual["something should be false"] = thing_to_test;

    // We forget the third test.

    assert.equal(actual, expected);
});

当我运行上面的测试时,我得到以下输出:

  1) should allow multiple tests

  0 passing (12ms)
  1 failing

  1)  should allow multiple tests:

      AssertionError: { 'something should be true': true,
  'something should be false': true } == { 'something should be true': true,
  'something should be false': false,
  'forgot this one': 1 }
      + expected - actual

       {
      -  "something should be false": true
      +  "forgot this one": 1
      +  "something should be false": false
         "something should be true": true
       }

      at Context.<anonymous> (test.js:22:12)