[rspec-users] Testing the <=> operator

David Chelimsky dchelimsky at gmail.com
Tue Mar 16 00:55:38 EDT 2010


On Mar 15, 2010, at 11:16 PM, Chris DiMartino wrote:

> I'm having trouble getting rspec to allow me to test my Comparable class.  I would like to test the <=> method, but rspec is giving me unexpected results:
> 
> @hand = Hand.new('R')
> @other_hand = Hand.new('S')
> @hand.should be > @other_hand
> 
> expected > #<RPS::Hand:0x7f2d93661490>, got #<RPS::Hand:0x7f2d936615d0 @guess="R">
> 
> Shouldn't that test be checking for the comparison operator?

Nope. Keep in mind that most operators in ruby are actually methods. When the example says:

	@hand.should be > @other_hand

Ruby evaluates that as this:

	@hand.should(be.>(@other_hand))

Which rspec then translates to this (more or less):

	@hand.>(@other_hand) ? pass : fail

That make sense?



More information about the rspec-users mailing list