mocha.js - Chai Mocha : Identify should assertion

标签 mocha.js should.js chai

我正在使用mochachai作为断言。

我在规范中有几个主张:

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/

相关文章:

javascript - 将 should.js 与 Coffeescript 结合使用

node.js - 找不到模块 './lib/should'

node.js - nodejs - 测试失败但回调被调用

javascript - 如何在node js中抛出错误并捕获它mocha

sass - 使用 babel-node 和 babel-istanbul 运行 mocha 测试时加载 css 时出错

typescript - Chai 期望使用 Typescript 抛出与相同异常不匹配的异常

javascript - 将参数传递给 Mocha

node.js - 使用 Mocha 和 Should.js 组织测试

typescript - Vue组件mocha单元测试时基类方法不存在

javascript - 如何解决Mocha中的 "TypeError: Cannot read property ' equal' of undefined"