Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec test if an attribute was updated

My model has a method that will update several attributes in a model from a remote resource. I want to test that with Rspec, but cannot find how to test whether a field was created or updated.

Some pseudocode to explain the question

def update_from_remote
  attributes = {}
  fields.each_key do |field_name|
    attributes[field_name] = scrape_from_remote field_name
  end
  update_attributes(attributes)
end

In reality this code is a lot more complex, but that will only clutter my question. A create_from_remote is rather similar, only that it does not call update_attributes but simply sets them and then saves the object.

I would like to test this. What I want, is a spec that tests whether the fields were updated or filled:

it 'should fill or set all local attributes on a new profile' do
  #how to test if a list of fields were updated in the Database?
end
it 'should update all local attributes on an existing profile' do
  #how to test if a list of fields were created in the Database?
end

I am using mongoId, but AFAIK that should not make much difference.

like image 724
berkes Avatar asked Dec 16 '25 20:12

berkes


1 Answers

I should have dug deeper in the MongoID documentation. There is a chapter on Dirty tracking which can be used:

it "should be updated"
  @something.field.should be_changed
end
like image 70
berkes Avatar answered Dec 19 '25 11:12

berkes