柴摩卡:确定应断言 [英] Chai, Mocha: Identify should assertion

查看:53
本文介绍了柴摩卡:确定应断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mochachai作为断言.

I'm using mocha and chai as assertions.

我在规范中有几个主张:

I have several assertions in my spec:

Exp1.should.be.true
Exp2.should.be.true
Exp3.should.be.true

如果其中之一失败,摩卡将写出"expected false is true".有没有办法识别它们?

If one of them fails mocha writes "expected false to be true". Is there a way to identify them?

使用expect我可以做到:

expect(Exp1, 'Exp1').to.be true

使用should可能会发生这种情况吗?

Is something like this possible with should?

推荐答案

显然should目前不支持自定义错误消息.

Apparently should does not support custom error messages at the moment.

您可以创建自己的帮助程序来设置消息:

You can create your own helper for setting the message:

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;
});

这篇关于柴摩卡:确定应断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆