如何在不破坏FactoryGirl的情况下将seeds.rb加载到测试数据库中?

时间:2012-03-25 14:50:41

标签: ruby-on-rails testing factory-bot

我尝试从底部列出的SO问题中获取解决方案,但我的问题是我使用的是Capybara和FactoryGirl,我似乎无法从任何地方加载seeds.rb而不会导致许多与种子数据完全分开的测试从打破。 大多数错误消息都是page.should_not have_content user.email的变体 在测试之后,我尝试删除我通过工厂制作的用户。这些测试在我加载种子数据之前就已经过了。

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?


我所拥有的是一个管理员组,分配了管理员权限,并将seeds.rb中的管理员用户链接在一起 一种可能性是在我的seeds.rb中调用一个工厂来填充这些数据,但我还没弄清楚如何。

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

测试失败

这在我加载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

2 个答案:

答案 0 :(得分:8)

我不知道原因,但是需要seeds.rb文件而不是在测试环境中加载它,你可以在需要它的测试上调用seed_data。

规格/ helpers.rb

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

更好的解决方案

规格/ spec_helper.rb

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

答案 1 :(得分:5)

Rails 4.2.0 RSpec 3.x 中,这就是我的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