Forum: RSpec Expecting nil or false

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-03-31 18:08
Hi,

I just ran into this issue. I have a method that returns: false, true,
nil or an object.

This method is used by another method to test for true/false. In Ruby
that's easy to handle as nil and false evaluate to false, and everything
else evaluates to true, but RSpec seems to expect an exact value such as
nil, true, false, not_nil, etc.

Do I have to rewrite my return values to always return true or false?
75a43ab10305dddce2b72827a5cbb0b9?d=identicon&s=25 Brian Colfer (briancolfer)
on 2009-03-31 18:46
(Received via mailing list)
What about variable.nil? == true

Sent from Brian's iPhone

On Mar 31, 2009, at 9:26 AM, "Fernando Perez" <lists@ruby-forum.com>
369b9fd2a0d9e7cdbc60907a2a056ad9?d=identicon&s=25 Scott Taylor (Guest)
on 2009-03-31 19:02
(Received via mailing list)
On Mar 31, 2009, at 12:08 PM, Fernando Perez wrote:

> nil, true, false, not_nil, etc.
Use the boolean matchers:

def foo?
   nil
end

obj.should be_foo #=> fails
obj.should_not be_foo  #=> passes

Scott
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-03-31 19:05
(Received via mailing list)
On Tue, Mar 31, 2009 at 11:08 AM, Fernando Perez <lists@ruby-forum.com>
wrote:
> Do I have to rewrite my return values to always return true or false?
No, but you may have to use different matchers. What are you using?
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-03-31 19:11
>> Do I have to rewrite my return values to always return true or false?
> No, but you may have to use different matchers. What are you using?

I use: should be_false

Rspec complains it receives nil when it is expecting false.
0be0e4aa42aacd9a8a95c792de273ca7?d=identicon&s=25 Aslak Hellesøy (aslakhellesoy)
on 2009-03-31 19:39
(Received via mailing list)
Den 31. mars. 2009 kl. 18.34 skrev "Colfer, Brian"
<bcolfer@shopping.com>:

> What about variable.nil? == true
>
No, that will not raise anything.

x.should == nil
Or
X.should be_nil
Ditto for false and true.

What have you been trying?

Aslak
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-03-31 19:46
(Received via mailing list)
On Tue, Mar 31, 2009 at 12:11 PM, Fernando Perez <lists@ruby-forum.com>
wrote:
>
>>> Do I have to rewrite my return values to always return true or false?
>> No, but you may have to use different matchers. What are you using?
>
> I use: should be_false
>
> Rspec complains it receives nil when it is expecting false.

As would be expected.

What is the full expression? i.e. what is it that should be false (or
nil)?
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-03-31 19:54
> What is the full expression? i.e. what is it that should be false (or
> nil)?

Basically:

def is_it_cool?
  find(blabla, :conditions => 'coolness > 1000')
end

In order to stay consistent and as the question mark suggests that true
or false will be returned, I have updated my method too:

def is_it_cool?
  !!find(...)
end


Is true that the dynamic aspect of Ruby allows us to not have to
statically type the return value of a method, so it could be an object,
nil or a boolean.

What do you think?
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-04-01 00:07
(Received via mailing list)
On Tue, Mar 31, 2009 at 12:54 PM, Fernando Perez <lists@ruby-forum.com>
wrote:
> or false will be returned, I have updated my method too:
> What do you think?
I don't know if you're familiar with the concept of truthiness, but
that is what you're talking about here. IMO, be_true should be
specific. I'm open to other opinions, and would consider changing the
way it works if we can do it without breaking everybody's stuff.

Somebody joked about having a be_truthy matcher. Perhaps he actually
wrote one.

As for how I handle these things now, I would do this:

something.should be_cool

def cool?
  !!find(...)
end

I find that almost every time I approach it this way, I land on a
predicate method that is ruby-ish. is_it_cool? is not ruby-ish. nor
would is_cool?

FWIW,
David
This topic is locked and can not be replied to.