DiyActiveRecordFixtures

July 09, 2012
DIY fixtures - forget factory_girl and their ilk. Usually in lib/fixtures/fixtures.rb because they can be used in rake tasks and other not-specifically-test places. Add requires in test_helper and then in whatever rake tasks or scripts use them.


require 'faker'

class BaseFixture
def self.create(opts={})
fixture = self.new(opts)
fixture.save!
fixture
end
end

class CustomerFixture < BaseFixture
def self.new(opts={})
Customer.new(opts.reverse_merge(:name => Faker::Name.name))
end
end


“seed.rb is broken” <- if you don't use this much, then that can happen. If you re-use DIY Fixtures in tests, seeding and exploratory testing - everywhere. Less likely to break.

tags: ComputersAndTechnology
comments powered by Disqus