[rspec-users] Testing equality

Juanma Cervera lists at ruby-forum.com
Fri Jul 9 03:19:31 EDT 2010


David Chelimsky wrote:
> On Jul 8, 2010, at 5:04 AM, Juanma Cervera wrote:
> 
>> end.
>> 
>> FAILS
>> 
>> This fails because the object expected is different from the object
>> gotten, and the only difference are the BigDecimal attributes, that are
>> different objects, even though they have the same value.
> 
> rspec delegates ==() to the objects being compared. It's up to library 
> providers and users to override ==() in a way that is sane in a given 
> context. Assuming a single BigDecimal attr named amount, something like 
> this:
> 
> def ==(other)
>   self.attributes.merge(:amount => BigDecimal.new(self.amount.to_s)) ==
>   other.attributes.merge(:amount => BigDecimal.new(other.amount.to_s))
> end
> 
> HTH,
> David

Thanks for your answer David, but I think there is a problem.
The assertion that fails has nothing to do with the BidDecimal types, 
but with the created_at and updated_at timestamps
I have try this:

v = Factory.create(:my_object)
f = MyObject.first

v.should == f ==> This fails
v.amount.should == f.amount  ==> This works


But...

v.updated_at.should == f.updated_at


RSpec::Expectations::ExpectationNotMetError: expected: Fri, 09 Jul 2010 
06:57:48 UTC +00:00,
     got: Fri, 09 Jul 2010 06:57:48 UTC +00:00 (using ==)
Diff:
  from 
/home/jmcervera/.rvm/gems/ruby-1.8.7-p299 at rails3/gems/rspec-expectations-2.0.0.beta.15/lib/rspec/expectations/fail_with.rb:29:in 
`fail_with'
  from 
/home/jmcervera/.rvm/gems/ruby-1.8.7-p299 at rails3/gems/rspec-expectations-2.0.0.beta.15/lib/rspec/matchers/operator_matcher.rb:39:in 
`fail_with_message'
  from 
/home/jmcervera/.rvm/gems/ruby-1.8.7-p299 at rails3/gems/rspec-expectations-2.0.0.beta.15/lib/rspec/matchers/operator_matcher.rb:61:in 
`__delegate_operator'
  from 
/home/jmcervera/.rvm/gems/ruby-1.8.7-p299 at rails3/gems/rspec-expectations-2.0.0.beta.15/lib/rspec/matchers/operator_matcher.rb:51:in 
`eval_match'
  from 
/home/jmcervera/.rvm/gems/ruby-1.8.7-p299 at rails3/gems/rspec-expectations-2.0.0.beta.15/lib/rspec/matchers/operator_matcher.rb:29:in 
`=='
  from (irb):41






-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list