4

I am using Ruby on Rails 3.0.9 and RSpec 2. I would like to auto-load seed data in the test database before I tesst my application. That is, at the testing start up time (when I run tests) I would like to "auto-populate"\"auto-boot" the test database.

How can I populate the database with seed data?


P.S.: As I read around, (maybe) I should populate the test database by adding some code to the /spec/spec_helper.rb file... but what code and how?

In my task/custom.rake file I have:

namespace :test do
  desc "Boot database"
  task :boot => [:load, :seed] do
  end

  desc "Reboot database"
  task :reboot => [:purge, :boot] do
  end
end
user502052
  • 14,803
  • 30
  • 109
  • 188

1 Answers1

3

If you'd like to just invoke the seed rake task, I'd do something like this:

ENV['RAILS_ENV'] = 'test'
Rake::Task["db:seed"].invoke

But, I'd recommend using Fixtures (http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures).

Or even better a fixture replacement like Factory Girl (https://github.com/thoughtbot/factory_girl), whereby you can load dummy data with all the associated data. This is a much less brittle approach.

sren
  • 3,513
  • 4
  • 25
  • 28
  • 1
    I am already using Fixtures and Factory Girl. I would like to populate just the test database (like I make for with development database). – user502052 Sep 18 '11 at 03:00
  • 1
    +1 for factory_girl. You can also combine it with [forgery](https://github.com/sevenwire/forgery) or [similar](http://ruby-toolbox.com/categories/random_data_generation.html) for even more fun. – talyric Sep 18 '11 at 03:09