繁体   English   中英

如何让 Mocha 在测试中失败

[英]How to get Mocha to fail a test

我有以下测试:

it.only('validation should fail', function(done) {
    var body = {
        title: "dffdasfsdfsdafddfsadsa",
        description: "Postman Description",
        beginDate: now.add(3, 'd').format(),
        endDate: now.add(4, 'd').format()
    }


    var rules = eventsValidation.eventCreationRules();
    var valMessages = eventsValidation.eventCreationMessages();

    indicative
        .validateAll(rules, body, valMessages)
        .then(function(data) {
            console.log("SHOULD NOT GET HERE");
            should.fail("should not get here");
            done();

        })
        .catch(function(error) {
            console.log("SHOULD GET HERE");
            console.log(error);
        });
    done();
});

测试执行路径正确。 当我验证数据时,它会显示“不应该到达这里”。 测试实际上是为了确保它没有。 当我输入非验证数据时,代码会转到“应该到达这里”。 所以验证规则有效。

我想要做的是确保当我有错误的验证数据时测试失败并且它验证。 但是,当我使用良好的数据运行它时,它会验证,运行失败,但 mocha 仍将其标记为通过。 如果执行达到“不应该到达这里”,我希望它失败。

我试过 throw new Error("fail"); 也没有运气。 在这两种情况下,它实际上似乎也在 .catch 块中运行代码。

有什么建议? 我在一个类似的问题中找到了解决方案。 写这个问题是因为这些解决方案似乎对我不起作用。

您可以调用assert.fail

it("should return empty set of tags", function()
{
    assert.fail("actual", "expected", "Error message");
});

此外,如果您使用参数调用done()函数,Mocha 会认为测试失败。

例如:

it("should return empty set of tags", function(done)
{
    done(new Error("Some error message here"));
});

虽然第一个对我来说看起来更清晰。

在 ES2017 async / await世界中, chai-as-promised不需要那么多。 尽管简单的拒绝是一个地方chai-as-promised仍然使用起来更整洁。

如果您想更详细地测试错误,则需要catch

it.only('validation should fail', async function(){
    let body = { ... }
    let rules = eventsValidation.eventCreationRules()
    let valMessages = eventsValidation.eventCreationMessages()

    try {
        await indicative.validateAll(rules, body, valMessages)
    } catch (error) {
        expect(error).to.be.instanceOf(Error)
        expect(error.message).to.match(/Oh no!/)
        return
    }
    expect.fail(null, null, 'validateAll did not reject with an error')
    // or throw new Error('validateAll did not reject with an error')
})

async / await需要Node.js 7.6+或像Babel这样的编译器

使用chai-as-promised和原生 Mocha 承诺处理程序。

var chai = require('chai').use(require('chai-as-promised'));
var should = chai.should(); // This will enable .should for promise assertions

你不再需要done ,只需返回承诺。

// Remove `done` from the line below
it.only('validation should fail', function(/* done */) {
    var body = {
        title: "dffdasfsdfsdafddfsadsa",
        description: "Postman Description",
        beginDate: now.add(3, 'd').format(),
        endDate: now.add(4, 'd').format()
    }

    var rules = eventsValidation.eventCreationRules();
    var valMessages = eventsValidation.eventCreationMessages();

    // Return the promise
    return indicative
        .validateAll(rules, body, valMessages)
        .should.be.rejected; // The test will pass only if the promise is rejected

    // Remove done, we no longer need it
    // done();
});

这个简单的投掷对我有用

describe('Lead', () => {
  it('should create a new lead', async () => {
     throw 'not implemented'
  })
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM