to.equal(true) 和 to.be.true 有什么区别?

What are the differences between to.equal(true) and to.be.true?

我正在学习 Mocha 和 Chai。

我尝试了解何时必须使用 to.equal(true)to.be.true 来了解在不同情况下哪个更好。

谢谢!

看起来没有什么不同

https://github.com/chaijs/chai/blob/master/lib/chai/core/assertions.js#L298

https://github.com/chaijs/chai/blob/master/lib/chai/core/assertions.js#L502

我对 docs 的理解是 .to.be 以及 Expect/Should 语法的其他各种部分只是语法糖,没有实际功能。

所以 .to.be.true === .true.to.equal(true) === .equal(true)。所以区别,如果有的话,是在 .true.equal(true) 之间——而且没有任何区别; .true 只是 .equal(true).

的句法 shorthand

他们测试的是同样的东西。换句话说,哪里.to.equal(true)失败了,.to.be.true也失败了,哪里.to.equal(true)成功了, .to.be.true.

也是

但是,它们确实不同,因为 .to.equal 接受可选的自定义错误消息,而 .to.be.true 不接受自定义错误消息。

var settings = {
    verbose: "foo"
};
settings.verbose.should.equal(true, "verbose setting");

将显示如下错误消息:

AssertionError: verbose setting: expected 'foo' to equal true

expect(settings.verbose).to.equal(true, "verbose setting") 相同。如果没有自定义错误消息,错误将是:

AssertionError: expected 'foo' to equal true

如果您使用 expect(settings.verbose).to.be.true("verbose setting"),测试将失败,但自定义错误消息将被忽略。