rspec and inequalities?

M

Mark Thomas

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)?
 
J

James H.

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
 
M

Mark Thomas

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.
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

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)
 
M

Mark Thomas

 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.

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?
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

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?
I don't think that would work. But I think you could use the rather Yoda
like:

item.valid?.should be
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top