微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – RSpec中“be_true”和“true”之间有什么区别?

有任何人可以用简单的例子来解释一下关于be_true和 Ruby间的区别的区别.我也看到be_true和be_false被改为be_truthy和be_falsey

我有一个例子,“真的”工作,但是当我试图使用’be_true’或’be_truthy’规格失败.

我正在使用RSpec版本3.1.7

解决方法

根据 this thread的be_true和be_false现在称为be_truthy和be_falsy.

在true和be_truthy之间的基本差异是false和be_falsy是be_falsy / be_truthy匹配器通过,如果“预期结果”(即任何对象)(为be_falsy)/不是(对于be_truthy)为零或为false,而在另一方面是真实的,并被假使用==用于验证您的“预期结果”(布尔对象)等于true / false.

rspec expectations’ truthiness起是什么意思

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

例子 –

为了真实而虚假:

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 / false,因为它们已经存在于上述示例中.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐