-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.4.txt
21 lines (13 loc) · 953 Bytes
/
13.4.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
13.4 Be True in the Eyes of Ruby
In Ruby, there are two values that evaluate to false in a Boolean expression. One of them is, of course, false. The other is nil. Every other value is evaluated as true. Even 0:
puts "0 evals to true" if 0
RSpec’s be_true and be_false matchers can be used to specify methods that should return values that Ruby will evaluate as true or false, as opposed to the actual values true and false:
true.should be_true
0.should be_true
"this".should be_true
false.should be_false
nil.should be_false
For the rare cases in which we care that methods return the values true or false, we can use the equal( ) matcher:
true.should equal(true)
false.should equal(false)
Up until now we’ve been discussing expectations about the state of an object. The object should be_in_some_state. But what about when the state we’re interested in is not in the object itself but in an object that it owns?