【问题标题】:How to provide chai expect with custom error message for mocha unit test?如何为 chai expect 提供 mocha 单元测试的自定义错误消息?
【发布时间】:2018-01-22 12:56:48
【问题描述】:

我有一个使用 chai 的期望的 mocha 测试:

it("should parse sails out of cache file", async () => {
    const sailExtractor = new Extractor();
    const result = await sailExtractor.extract("test.xml");

    try {
        expect(result.length).to.be.greaterThan(0);
        const withMandatoryFlight = result.filter((cruises) => {
            return cruises.hasMandatoryFlight === true;
        });
        expect(withMandatoryFlight.length).to.be.greaterThan(0);
        const cruiseOnly = result.filter((cruises) => {
            return cruises.hasMandatoryFlight === false;
        });

        expect(cruiseOnly.length).to.be.greaterThan(0);

        return Promise.resolve();
    } catch (e) {
        return Promise.reject(e);
    }
}

现在如果一个to.be.greaterThan(0) 期望失败,mocha 上的错误输出对开发人员不友好:

 AssertionError: expected 0 to be above 0
      at Assertion.assertAbove (node_modules/chai/lib/chai/core/assertions.js:571:12)
      at Assertion.ctx.(anonymous function) [as greaterThan] (node_modules/chai/lib/chai/utils/addMethod.js:41:25)
      at _callee2$ (tests/unit/operator/croisiEurope/CroisXmlExtractorTest.js:409:61)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:65:40)
      at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:303:22)
      at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:117:21)
      at fulfilled (node_modules/tslib/tslib.js:93:62)
      at <anonymous>
      at process._tickDomainCallback (internal/process/next_tick.js:228:7)

我想用更人性化的东西来代替它。有没有办法告诉 chai 使用自定义错误消息?

我希望能够像这个伪代码一样使用它:

 expect(result.length)
      .to.be.greaterThan(0)
      .withErrorMessage("It should parse at least one sail out of the flatfile, but result is empty");

然后应该打印失败的摩卡错误:

 AssertionError: It should parse at least one sail out of the flatfile, but result is empty

【问题讨论】:

    标签: javascript node.js typescript chai


    【解决方案1】:

    如果您将should 用于您的断言,您可以将一个字符串传递给测试函数,如果条件失败,该字符串将被写出。例如:

    result.length.should.be.above(0, "It should parse at least one sail out of the flatfile, but result is empty");
    

    我不确定这是否可以通过expect 实现。 API 似乎没有提及它。

    【讨论】:

    • 这对我不起作用,但 chai 期望自定义消息是。
    • 我意识到这已经过时了,但只是为了确认这个 确实 对我有用,语法为 myCollection.should.have.length(1, "oopsy");。奇怪的是,@liminal18 没有。感谢您的回答!
    【解决方案2】:

    每个expect 方法都接受一个可选参数message

    expect(1).to.be.above(2, 'nooo why fail??');
    expect(1, 'nooo why fail??').to.be.above(2);
    

    所以,在你的情况下应该是:

    expect(result.length)
      .to.be.greaterThan(0, "It should parse at least one sail out of the flatfile, but result is empty");
    

    【讨论】:

    • OP 正在使用chai,但看起来您的答案也适用于该库。或者,您可以使用expect(result.length, 'It should parse...').to.be.greaterThan(0)
    • 旁注:由于我使用的是打字稿,我可以通过跳入greaterThan方法自己找到一个,因为它的打字被声明为NumberComparer interface,并且有可选的消息参数显示出来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2018-02-13
    • 2018-10-02
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多