Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have tried getting solutions from the SO questions listed at the bottom but my problem is that I am using Capybara and FactoryGirl and I cannot seem to load seeds.rb from anywhere without causing many tests that are completely separate from the seed data from breaking. Most of the error messages are variations on page.should_not have_content user.email after a test where I try delete a user that I made through a factory. These are tests that passed fine until I loaded the seed data.

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

Prevent Rails test from deleting seed data

What is the best way to seed a database in Rails?

How to auto-load data in the test database before to test my application?


What I have is a single admin group, assigned the admin permission and an admin user in the seeds.rb linked together One possibility is calling a factory in my seeds.rb to populate this data but I have not yet figured out how.

seeds.rb

User.find_or_create_by_email(email: "[email protected]",
                             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("[email protected]")
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

Failing test

This passes before I load 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
like image 612
BookOfGreg Avatar asked Dec 09 '22 01:12

BookOfGreg


1 Answers

I do not know the reason but requiring the seeds.rb file instead of loading it in the test environment works and you can just call seed_data on the tests that need it.

spec/helpers.rb

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

better solution

spec/spec_helper.rb

RSpec.configure do |config|
  config.before(:suite) do
    require "#{Rails.root}/db/seeds.rb"
  end
  ........
end
like image 198
BookOfGreg Avatar answered Apr 26 '23 07:04

BookOfGreg