“be_true"和“be_true"有什么区别?和“真实"在 RSpec [英] What is the difference between "be_true" and "be true" in RSpec
问题描述
谁能用简单的例子解释一下Ruby中be_true
和be true
之间的区别.我也看到 be_true
和 be_false
变成了 be_truthy
和 be_falsey
Can any one please explain me about the difference between be_true
and be true
in Ruby with simple example. I have also seen be_true
and be_false
are changed to be_truthy
and be_falsey
我有一个 'be_true'
工作的例子,但是当我尝试使用 'be_true'
或 'be_truthy'
规范失败时.
I have an example where 'be true'
worked, but when I tried to use 'be_true'
or 'be_truthy'
spec failed.
我使用的是 RSpec 3.1.7 版
I am using RSpec version 3.1.7
推荐答案
根据 this thread be_true
和 be_false
现在被称为 be_truthy
和 be_falsy
.
According to this thread be_true
and be_false
are now known as be_truthy
and be_falsy
.
be true
和 be_truthy
或 be false
和 be_falsy
之间的基本区别在于 be_falsy
/be_truthy
如果预期结果"(即任何对象)is(对于 be_falsy
)/不是(对于be_truthy
)nil
或 false
,而另一方面 be true
和 be false
使用 ==
来验证您的预期结果"(布尔对象)等于true
/false
.
The basic difference between be true
and be_truthy
or be false
and be_falsy
is that be_falsy
/be_truthy
matcher passes if the "expected result"(i.e. any object) is(for be_falsy
)/ is not(for be_truthy
) nil
or false
, while on the other hand be true
and be false
use ==
for validating your "expected result"(boolean object) to be equal to true
/ false
.
rspec 期望的真实性 意味着:
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
除了 nil
/true
/ 之外,您可以在
,因为它们已经出现在上面显示的示例中."string"
位置使用任何其他对象作为预期结果"false
You can use any other object as "expected result" at the place of "string"
except nil
/true
/false
, because they are already present in the examples shown above.
这篇关于“be_true"和“be_true"有什么区别?和“真实"在 RSpec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!