I had a problem recently where i was using the test object-generating plugin Machinist to make a blueprint for one of my model classes. The model has a before_create filter which references some other data in the database, which didn’t exist in my test version of the db. This before_create filter had nothing to do with the tests i was running and i wanted to just stub it out. I couldn’t work out how, so wrote a wrapper method that looked like this:
def make_music_service(attributes = {})
music_service = MusicService.make_unsaved(attributes)
music_service.stub!(:set_default_column_customisations).and_return(true)
music_service.save
music_service
end
That meant that whenever i wanted to make a MusicService in my tests i had to call this method instead. Pretty clumsy, eh. But – Machinist creator Pete Yandell came through with a great bit of info on the machinist mailing list – when you’re inside your blueprint, you can reference the object being made with ‘object’. So, now i can put the stub inside the blueprint, like so:
MusicService.blueprint do
name { Faker::Name.name }
address1 { Faker::Address.street_address }
address2 { Faker::Address.street_address }
address3 { Faker::Address.city }
postcode { Faker::Address.uk_postcode }
default_password { "password" }
object.stub!(:set_default_column_customisations).and_return(true)
end
Thanks Pete!
Thanks this totally helped me with getting the restful auth working to shut off the observer that was sending out the email