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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With