Using Seed Data with RSpec Stories
on July 17, 2008 @ 02:29 PM
Yesterday I needed to introduce seed data into our application. In order for our story suite to continue to run I needed to populate seed data into the testing environment when the stories ran. I didn’t want to use the production seed data in my test environment, but I wanted to keep things simple. I am already using using seed_fu from Michael Bleigh to handle the seed data for production. Why not leverage it for stories as well?
I made a minor modification to the task to support passing in an argument to rake or setting an environment variable so you can load “fixtures” from a different directory. In my “stories/helper.rb” file I have added near the top:
system "rake db:seed FIXTURE_PATH=stories/fixtures" |
This loads the seed data from “RAILS_ROOT/stories/fixtures”. Here’s the modified seed_fu rake task:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
namespace :db do desc "Loads seed data from db/fixtures for the current environment." task :seed => :environment do fixture_path = ENV["FIXTURE_PATH"] ? ENV["FIXTURE_PATH"] : "db/fixtures" Dir[File.join(RAILS_ROOT, fixture_path, '*.rb')].sort.each { |fixture| puts "\n== Seeding from #{File.split(fixture).last} " + \ ("=" * (60 - (17 + File.split(fixture).last.length))) load fixture puts "=" * 60 + "\n" } Dir[File.join(RAILS_ROOT, fixture_path, RAILS_ENV, '*.rb')].sort.each { |fixture| puts "\n== [#{RAILS_ENV}] Seeding from #{File.split(fixture).last} " + \ ("=" * (60 - (20 + File.split(fixture).last.length + RAILS_ENV.length))) load fixture puts "=" * 60 + "\n" } end end |
After this my seed data was resembling the typical format:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
ExpenseCategory.transaction do ExpenseCategory.seed(:object_code) do |s| s.object_code = "1234" s.name = "Foo" end ExpenseCategory.seed(:object_code) do |s| s.object_code = "1235" s.name = "Bar" end ExpenseCategory.seed(:object_code) do |s| s.object_code = "1236" s.name = "Baz" end end |
And while I like this it’s a little verbose. So I modified seed_fu to introduce the seed_many method to consolidate that:
1 2 3 4 5 6 7 |
ExpenseCategory.transaction do ExpenseCategory.seed_many(:object_code, [ { :object_code => "1234", :name => "Foo"}, { :object_code => "1235", :name => "Bar"}, { :object_code => "1236", :name => "Baz"} ]) end |
It maintains a simple API and highly readable seed data. I’ve committed this to my fork of seed_fu, and am hoping that Michael accepts the patch and adds it to seed_fu.
- The patch for the rake FIXTURE_PATH: http://github.com/zdennis/seed-fu/commit/4d391aa214c369f161e37c194b07d49e55312b35
- The patch to add the seed_many method: http://github.com/zdennis/seed-fu/commit/9f7e8b27b7cb175911474a898ea282771ab2a5c3



0 comments
Jump to comment form | comments rss [?]