The Spaghetti Refactory Established 2015

Reload stale objects in finnicky Rails rspec tests

I have had plenty of times where an rspec test was failing because an object was still showing an old value after an update. This seems to happen most often in Capybara. I know I'm not technically supposed to be checking object data in Capybara tests, just checking what's actually seen on the page, but there are some times where I need to be extra sure, or where the data being updated isn't showing on the page.

In rspec, call #reload on an object if it has changed in the database in order to sync and get the most recent changes.

NOTE: The object must have an ID, because under the hood it is just running
object = Object.find( # => 'Frank'
object.update(name: 'Tank') # => 'Frank'
object.reload # => 'Tank'