柴,摩卡:确定应该断言

WHI*_*LOR 5 mocha.js should.js chai

我正在使用mochachai作为断言.

我的规范中有几个断言:

Exp1.should.be.true
Exp2.should.be.true
Exp3.should.be.true
Run Code Online (Sandbox Code Playgroud)

如果其中一个失败,则mocha会写"预期错误为真".有没有办法识别它们?

随着expect我能做到这一点:

expect(Exp1, 'Exp1').to.be true
Run Code Online (Sandbox Code Playgroud)

这样的事情可能should吗?

Mir*_*toš 5

显然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;
});
Run Code Online (Sandbox Code Playgroud)