How can I load seeds.rb into the test database without breaking FactoryGirl?

I tried to get solutions from the SO questions listed below, but my problem is that I am using Capybara and FactoryGirl and I cannot load seeds.rb from anywhere without calling a lot of tests that are completely separate from the seed data from breakage. Most of the error messages are options on the page.should_not have_content

after test, where I am trying to delete a user that I made through a factory. These are tests that went fine until I loaded the seed data.

How to load db: raw data into test database automatically?

Prevent Rails Validation from Seed Data

What's the best way to seed a database in Rails?

How do I automatically load data into a test database before testing my application?

What I have is a separate admin group that is assigned admin permission and admin user on the seeds.rb connection One possibility is to call a factory in my seeds.rb to populate this data, but I haven't figured out how this is yet to do.


User.find_or_create_by_email(email: "admin@admin.admin",
                             password: "admin", password_confirmation: "admin")
%w{admin supermod}.each {|w| Group.find_or_create_by_name(w)}
%w{admin mod player}.each {|w| Permission.find_or_create_by_name(w)}
g = Group.find_by_name("admin")
g.permission_id = Permission.find_by_name("admin").id
puts "failed to add admin permission to admin group" unless
u = User.find_by_email("")
ug =
ug.group_id = Group.find_by_name("admin").id
ug.user_id =
puts "failed to add admin group to #{}" unless &&


Failed test

This is done before loading seeds.rb

it "lets you remove user from group" do
  user = Factory.create(:user)
  admin = admin_login
  group = add_group
  add_user_to_group user, group
  click_link "delete_#{}"
  page.should_not have_content

def admin_login
  admin =
  visit login_path
  fill_in "email", :with =>
  fill_in "password", :with => admin.password
  click_button "Log In"
  return admin
def add_group
  group =
  visit new_group_path
  fill_in "group_name", :with =>
  click_button "Submit"
  return group
def add_user_to_group (user, group)
  visit groups_path
  click_link "#{}_users"
  fill_in "user_email", :with =>
  click_on "Add User"



source to share

2 answers

I don't know the reason, but it requires the seeds.rb file to work instead of loading it into the test environment, and you can just call seed_data for the tests that need it.

specs /helpers.rb

def seed_data
  require "#{Rails.root}/db/seeds.rb"


The best decision

spec / spec _helper.rb

RSpec.configure do |config|
  config.before(:suite) do
    require "#{Rails.root}/db/seeds.rb"




In Rails 4.2.0 and RSpec 3.x it looks like rails_helper.rb.

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  # 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.before(:suite) do

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation

  config.before(:each) do

  config.after(:each) do

  config.before(:all) do
    Rails.application.load_seed # loading seeds




All Articles