我有以下测试:
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();
});
测试执行路径正确。当我有验证数据时,它转到“不应该在这里找到”。该测试实际上是为了确保不会。当我输入非验证数据时,代码确实转到“应在此处获取”。因此,验证规则起作用。
我要尝试做的是确保当我有不好的验证数据并验证时测试失败。但是,当我使用良好的数据按原样运行它时,它会验证,运行失败,但摩卡仍将其标记为通过。如果执行变为“ SHOULD NOT GET HERE”,我希望它失败。
我试过抛出新的Error(“ fail”); 也没有运气。在这两种情况下,它实际上似乎也都在.catch块中运行代码。
有什么建议?我在类似的问题中找到了解决方案。之所以写这个问题,是因为这些解决方案似乎对我不起作用。
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();
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句