我正在使用mocha
和chai
作为断言。
我在规范中有几个主张:
Exp1.should.be.true
Exp2.should.be.true
Exp3.should.be.true
如果其中之一失败,则 Mocha 会写“expected false is true”。有没有办法识别它们?
使用
expect
我可以做到:expect(Exp1, 'Exp1').to.be true
should
可能会发生这种情况吗?
最佳答案
显然should
目前不支持自定义错误消息。
您可以创建自己的帮助程序来设置消息:
var chai = require('chai'),
should = chai.should();
// Helper definition - should be in a shared file
chai.use(function(_chai, utils) {
_chai.Assertion.addMethod('withMessage', function(msg) {
utils.flag(this, 'message', msg);
});
});
// Sample usage
it('should fail', function() {
var Exp1 = false;
var Exp2 = false;
Exp1.should.be.withMessage('Exp1').true;
Exp1.should.withMessage('Exp2').be.true;
});
关于mocha.js - Chai Mocha : Identify should assertion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11527474/