What is the difference between "be_true" and "be true" in RSpec
Asked Answered
H

1

25

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

I have an example where 'be true' worked, but when I tried to use 'be_true' or 'be_truthy' spec failed.

I am using RSpec version 3.1.7

Historic answered 6/11, 2014 at 12:51 Comment(0)
P
49

According to this thread be_true and be_false are now known as be_truthy and be_falsy.

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.

What it means from rspec expectations' truthiness is:

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

Examples -

For be true and 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

For be_truthy and 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

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.

Pori answered 6/11, 2014 at 13:20 Comment(6)
when you say (be_truthy is true when not nil or false) you're kind of suggesting the wrong thing. Although your answer is correct and most of us get the idea, it's logically incorrect to say that, and should be modified to (be_truthy is true when not nil nor false). Or (be_truthy is true when not (nil or false)).Bobinette
@Bobinette : I didn't understand your point. Could you point out where I said "be_truthy is true when not nil or false"? I would very much like to clarify if there's a misinterpretation that might have happened in the answer. :)Pori
second paragraph, end of second line.Bobinette
@Bobinette : this one: "while on the other hand be true and be false use == for validating your "expected result" to be equal to true/false."?Pori
"be_falsy/be_truthy checks if the "expected result" is(for falsy)/is not(for truthy) nil or false"Bobinette
@Bobinette : I see, the statement I made tries to say is: be_truthy passes if "expected result" is any object other than nil or false. It's just that I've put it that way. Will edit it later.Pori

© 2022 - 2024 — McMap. All rights reserved.