我有以下测试:
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 block 中的代码。
有什么建议吗? 我在 similar question 中找到了解决方案.写这个问题是因为这些解决方案似乎对我不起作用。
最佳答案
您可以调用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"));
});
虽然第一个对我来说看起来更清晰。
关于javascript - 如何让 Mocha 考试不及格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30874676/