RSpec中“be_true”和“be true”之间的区别是什么?

时间:2014-11-06 12:51:11

标签: ruby-on-rails ruby rspec rspec3

任何人都可以通过简单的例子向我解释Ruby中be_truebe true之间的区别。我还看到be_truebe_false已更改为be_truthybe_falsey

我有一个'be true'工作的示例,但是当我尝试使用'be_true''be_truthy'规范失败时。

我正在使用RSpec 3.1.7版

1 个答案:

答案 0 :(得分:28)

根据this thread be_truebe_false现在称为be_truthybe_falsy

be truebe_truthybe falsebe_falsy之间的基本区别在于be_falsy / be_truthy匹配器通过&#34 ;预期结果"(即任何对象) (对于 be_falsy )/ 不是 (对于 be_truthy nilfalse,而另一方面be true和{{1}使用be false验证您的"预期结果"(布尔对象)等于== / true

rspec expectations' truthiness的含义是:

false

示例 -

expect(actual).to be_truthy # passes if actual is truthy (not nil or false) expect(actual).to be true # passes if actual == true expect(actual).to be_falsy # passes if actual is falsy (nil or false) expect(actual).to be false # passes if actual == false expect(actual).to be_nil # passes if actual is nil expect(actual).to_not be_nil # passes if actual is not nil be true

be false

it { expect(true).to be true } # passes it { expect("string").to be true } # fails it { expect(nil).to be true } # fails it { expect(false).to be true } # fails it { expect(false).to be false } # passes it { expect("string").to be false} # fails it { expect(nil).to be false} # fails it { expect(true).to be false} # fails be_truthy

be_falsy

您可以使用任何其他对象作为"预期结果"在it { expect(true).to be_truthy } # passes it { expect("string").to be_truthy } # passes it { expect(nil).to be_truthy } # fails it { expect(false).to be_truthy } # fails it { expect(false).to be_falsy } # passes it { expect(nil).to be_falsy } # passes it { expect("string").to be_falsy} # fails it { expect(true).to be_falsy } # fails "string" / nil / true之外的地方,因为它们已经出现在上面显示的示例中。

相关问题