Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 564
Geo Avatar asked Dec 06 '22 14:12

Geo


1 Answers

There is another way indeed. You can do it like this :

it "should not change sth" do
  expect {
    # some action
  }.to_not change{subject.attribute}
end

See https://www.relishapp.com/rspec/rspec-expectations/v/2-0/docs/matchers/expect-change.

like image 50
tomferon Avatar answered Dec 30 '22 08:12

tomferon