# Rspec and inequalities?

**URL:** <https://www.ruby-forum.com/t/rspec-and-inequalities/139947>\
**Category:** Ruby\
**Created:** [June 9, 2008, 1:40pm UTC](https://www.ruby-forum.com/t/rspec-and-inequalities/139947 "2008-06-09T13:40:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [June 9, 2008, 1:40pm UTC](https://www.ruby-forum.com/t/rspec-and-inequalities/139947/1 "2008-06-09T13:40:46Z")

</div>

In an old version of rspec, you could do “x.should\_be \< y”, but this  
throws a NoMethodError now. What’s the proper way to test inequalities  
(less than/greater than)?

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [June 9, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/rspec-and-inequalities/139947/2 "2008-06-09T17:30:53Z")

</div>

On Jun 9, 7:35 am, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In an old version of rspec, you could do “x.should\_be \< y”, but this  
> throws a NoMethodError now. What’s the proper way to test inequalities  
> (less than/greater than)?

It’s basically the same:

x.should be \< y

James

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [June 9, 2008, 5:55pm UTC](https://www.ruby-forum.com/t/rspec-and-inequalities/139947/3 "2008-06-09T17:55:42Z")

</div>

> It’s basically the same:
> 
> x.should be \< y
> 
> James

Thanks! Is this syntactic sugar for all be\_xxx methods? I just noticed  
that I can omit the underscore in be\_true also. I’m surprised I  
haven’t noticed that in examples.

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [June 12, 2008, 12:32am UTC](https://www.ruby-forum.com/t/rspec-and-inequalities/139947/4 "2008-06-12T00:32:20Z")

</div>

On Mon, Jun 9, 2008 at 11:54 AM, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > It’s basically the same:
> > 
> > x.should be \< y
> > 
> > James
> 
> Thanks! Is this syntactic sugar for all be\_xxx methods?

No.

1.should be \> 0  
will work, but say:

1.should be\_false \> 0

will fail when RSpec tries to send #:0? to 1. One might say that this is  
an  
rspec bug, but I’d say that 1.should be\_false \> 0 is nonsensical, so who  
cares.

> I just noticed  
> that I can omit the underscore in be\_true also. I’m surprised I  
> haven’t noticed that in examples.

This is because

x.should be y

is interpreted as

x.should eql y

so it will succeed if x.eql?(y) returns a truthy value.

Also note that there’s a difference between

x.should be\_true  
or  
x.should be true  
or  
x.should eql true

and

x.should be

The first three will succeed only iff x == true whereas the latter will  
succeed iff x is any truthy value (i.e. anything except nil and false),  
likewise

x.should\_not be

will succeed iff x is a falsy value (i.e. nil or false)

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [June 12, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/rspec-and-inequalities/139947/5 "2008-06-12T23:41:48Z")

</div>

On Thu, Jun 12, 2008 at 10:44 AM, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> item.should be valid?

I don’t think that would work. But I think you could use the rather  
Yoda  
like:

item.valid?.should be

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [June 12, 2008, 4:48pm UTC](https://www.ruby-forum.com/t/rspec-and-inequalities/139947/6 "2008-06-12T16:48:19Z")

</div>

On Jun 11, 6:31 pm, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> rspec bug, but I’d say that 1.should be\_false \> 0 is nonsensical, so who  
> cares.

Actually, what I meant is can  
item.should be\_valid

be replaced with  
item.should be valid

but now that I think about it, rspec probably does some method\_missing  
magic with the be\_ prefix and wouldn’t know to use #valid? with the  
question mark. Maybe the equivalent is

item.should be valid?
