[rspec-users] Testing equality

David Chelimsky dchelimsky at gmail.com
Thu Jul 8 07:23:47 EDT 2010

On Jul 8, 2010, at 5:04 AM, Juanma Cervera wrote:

> Hello
> How can I test equality with two objects when they include some
> attribute that is BigDecimal?
> if I make something like this:
> it "should ...whatever" do
>  obj = Factory.create(:my_object)
>  ...
>  MyObject.first.should == obj
> end.
> 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))


More information about the rspec-users mailing list