[rspec-users] got/expects causes mental layering violation

Jay Levitt lists-rspec at shopwatch.org
Thu Aug 2 16:10:48 EDT 2007

David Chelimsky wrote:
> On 8/2/07, Jay Levitt <lists-rspec at shopwatch.org> wrote:
>> I was, for the first time, spec'ing a class that redefined ==.  And my
>> spec was incorrect, so == was returning false.
>> The result was something like:
>> class C
>>    def ==(other)
>>      false
>>    end
>> end
>> .. C.new.should == other...
>> expected other, got #<C:0x7f03c454> (using ==)
> What did it really call other? The output should be evaluating a
> variable there, not naming it.

Right.. sorry, I over-simplified the example.  It's more like:

# class
class C
   def initialize(name, value)
     @name = name
     @value = value

   def ==(other)
     @value == other

# spec
c = C.new("name", 0)
c.should == 1

# output
expected 1, got #<C:0x7f03c454, @value=0, @name="name"> (using ==)

And that's confusing at first glance, because it implies that it was 
trying to compare 1 to the whole object, when obviously the redefined == 
function would compare it to C.value (which == 0)!

In an ideal world, rspec would tell me "expected 1, got 0".  But that, 
of course, is impossible without reverse-engineering an == function to 
see what it's really doing.

I'm not sure there is an actual solution, just throwing it out there to 
see if my confusion was common...


More information about the rspec-users mailing list