Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-load the seed data from db/seeds.rb with rake

I'm using rails-rspec gem and I have several specs (models, controllers, etc). When I run:

bundle exec rake

everything is tested. However, I would like to improve my specs by seeding some data (from db/seeds.rb) just after the database is created (in test environment).

My spec/spec_helper.rb file looks like this:

ENV["RAILS_ENV"] ||= 'test'

require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'ruby-debug'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.mock_with :rspec

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = false

  config.include SpecHelper

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.start
    stub_xmpp_rest_client!  
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

  config.include Devise::TestHelpers, :type => :controller
  config.include Delorean
  config.after(:each) { back_to_the_present }
  config.include Factory::Syntax::Methods
  config.extend ControllerMacros, :type => :controller
end

What could do the best way to do so? Thanks.

like image 686
Zag zag.. Avatar asked Dec 05 '11 14:12

Zag zag..


People also ask

What is db seeds rb?

The seeds.rb file is where the seed data is stored, but you need to run the appropriate rake task to actually use the seed data. Using rake -T in your project directory shows information about following tasks: rake db:seed. Load the seed data from db/seeds.rb. rake db:setup.

What does DB seed do?

Database seeding is populating a database with an initial set of data. It's common to load seed data such as initial user accounts or dummy data upon initial setup of an application.

How do I run seeds in rails?

When the database is not ready yet, in other words, we want to create the database first, then we can go with rails db:setup to first run migrations and then seeding. And the last option is when we want to reset the database, rails db:reset will drop the database, create again, and seed the application.


2 Answers

Bad idea! Never, ever, seed your test database. Use factories to create, within each test, only the records necessary for that test to pass. Seeding the test database will make your tests less reliable, because you'll be making lots of assumptions that aren't explicitly stated in your tests.

like image 79
Marnen Laibow-Koser Avatar answered Nov 06 '22 08:11

Marnen Laibow-Koser


Depending on how your seed file is configured, you might just be able to load/run it from a before(:each) or before(:all) block:

load Rails.root + "db/seeds.rb" 
like image 30
Taryn East Avatar answered Nov 06 '22 09:11

Taryn East