Reputation: 3716
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.
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
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
Upvotes: 4
Views: 6076
Reputation: 2137
In Rails 4.2.0 and RSpec 3.x, this is how my rails_helper.rb looks.
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
Upvotes: 5
Reputation: 3716
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.
def seed_data
require "#{Rails.root}/db/seeds.rb"
end
RSpec.configure do |config|
config.before(:suite) do
require "#{Rails.root}/db/seeds.rb"
end
........
end
Upvotes: 9