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 user.email

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.

seeds.rb

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 g.save
u = User.find_by_email("neonmd@hotmail.co.uk")
ug = UserGroup.new
ug.group_id = Group.find_by_name("admin").id
ug.user_id = u.id
puts "failed to add admin group to #{u.name}" unless u.save && ug.save

      

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_#{user.email}"
  page.should_not have_content user.email
end

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

      

+3


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"
end

      




The best decision

spec / spec _helper.rb

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

      

+8


source


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
    DatabaseCleaner.clean_with(:truncation)
  end

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

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

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

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

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

      

+5


source







All Articles