Search code examples
ruby-on-railsrspec

How can I test that no attribute on a model has been modified using rspec?


I want to check if no attributes on an ActiveRecord object have been modified. Currently I'm doing this:

prev_attr = obj.attributes <- this will give me back a Hash with attr name and attr value

And then, later, I grab the attributes again and compare the 2 hashes. Is there another way?


Solution

  • You should just be able to use equality matchers - does this not work for you?

    a = { :test => "a" }
    b = { :test => "b" }
    $ a == b
    => false
    b = { :test => "a" }
    $ a == b
    => true
    

    Or to use your example:

    original_attributes = obj.attributes
    # do something that should *not* manipulate obj
    new_attributes = obj.attributes
    new_attributes.should eql original_attributes