Fixtures, mock objects or in-memory ActiveRecord objects?
Wilson Bilkovich has posted an article about mocking ActiveRecord objects.
A new method mock_model
is defined that builds a mock object which will respond the same way as a real ActiveRecord
object. As I understand it, this means he can replace…
@campaign = mock("campaign")
@campaign.stub!(:is_a?).and_return(true)
@campaign.stub!(:new_record?).and_return(false)
@campaign.stub!(:id).and_return(rand(1000))
with…
mock_model :campaign
Although I agree with him that using fixtures is not a good idea, why not use a real ActiveRecord
object…
@campaign = Campaign.new
Sometimes due to the way ActiveRecord
couples your models to the database, it becomes essential to have a model in the database and not just in memory. In which case why not just do this…
@campaign = Campaign.create!