Javascript 如何让 Mocha 在测试中失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30874676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Mocha to fail a test
提问by Pompey Magnus
I have the following 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();
});
The test execution path is correct. When I have validating data, it goes to "SHOULD NOT GET HERE". The test is really to make sure it doesn't. And when I put in non validating data the code does go to "SHOULD GET HERE". So the validation rules work.
测试执行路径正确。当我验证数据时,它会显示“不应该到达这里”。测试实际上是为了确保它没有。当我输入非验证数据时,代码会转到“应该到达这里”。所以验证规则有效。
What I'm trying to do is make sure the test fails when when I have bad validation data and it validates. However when I run it as it is with good data it validates, runs the fail, but mocha still marks it as the passing. I want it to fail if the execution gets to "SHOULD NOT GET HERE".
我想要做的是确保当我有错误的验证数据时测试失败并且它验证。但是,当我使用良好的数据运行它时,它会验证,运行失败,但 mocha 仍将其标记为通过。如果执行达到“不应该到达这里”,我希望它失败。
I've tried throw new Error("fail"); as well with no luck. In both cases it actually seems to run the code in the .catch block as well.
我试过 throw new Error("fail"); 也没有运气。在这两种情况下,它实际上似乎也在 .catch 块中运行代码。
Any suggestions? I found solutions for this in a similar question. This question is written because those solutions don't seem to be working for me.
有什么建议?我在一个类似的问题中找到了解决方案。写这个问题是因为这些解决方案似乎对我不起作用。
采纳答案by Yuri Zarubin
Use chai-as-promised, with native Mocha promise handlers.
使用chai-as-promised, 与本机 Mocha 承诺处理程序。
var chai = require('chai').use(require('chai-as-promised'));
var should = chai.should(); // This will enable .should for promise assertions
You no longer need done, simply return the promise.
你不再需要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();
});
回答by MondKin
You can call assert.fail:
您可以致电assert.fail:
it("should return empty set of tags", function()
{
assert.fail("actual", "expected", "Error message");
});
Also, Mocha considers the test has failed if you call the done()function with a parameter.
此外,如果您done()使用参数调用该函数,Mocha 会认为测试失败。
For example:
例如:
it("should return empty set of tags", function(done)
{
done(new Error("Some error message here"));
});
Though the first one looks clearer to me.
虽然第一个对我来说看起来更清晰。
回答by Matt
In the ES2017 async/awaitworld chai-as-promisedis not needed as much. Although simple rejections are one place chai-as-promisedremains a little neater to use, a catchis required if you want to test the error in more detail.
在 ES2017 async/ awaitworldchai-as-promised中不需要那么多。尽管简单的拒绝是一个地方chai-as-promised仍然使用起来更整洁,但catch如果您想更详细地测试错误,则需要a 。
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/awaitrequires Node.js 7.6+or a compiler like Babel
async/await需要Node.js 7.6+或像Babel这样的编译器
回答by Alex Smirnov
This simple throw is working for me
这个简单的投掷对我有用
describe('Lead', () => {
it('should create a new lead', async () => {
throw 'not implemented'
})
})

