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

Ruby on rails 如何在不破坏FactoryGirl的情况下将seeds.rb加载到测试数据库中?,ruby-on-rails,testing,factory-bot,Ruby On Rails,Testing,Factory Bot,我已经尝试从底部列出的SO问题中获得解决方案,但我的问题是,我使用的是Capybara和FactoryGirl,我似乎无法从任何地方加载seed.rb,而不会导致许多与种子数据完全分离的测试。 大多数错误消息都是页面上的变体。用户不应该有内容。电子邮件 在一次测试之后,我尝试删除通过工厂创建的用户。在我加载种子数据之前,这些测试都顺利通过 我只有一个管理员组,分配了管理员权限,并在seeds.rb中有一个管理员用户链接在一起 一种可能是在myseeds.rb中调用一个工厂来填充此数据,

我已经尝试从底部列出的SO问题中获得解决方案,但我的问题是,我使用的是Capybara和FactoryGirl,我似乎无法从任何地方加载seed.rb,而不会导致许多与种子数据完全分离的测试。 大多数错误消息都是
页面上的变体。用户不应该有内容。电子邮件

在一次测试之后,我尝试删除通过工厂创建的用户。在我加载种子数据之前,这些测试都顺利通过


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

种子.rb 失败测试 这将在我加载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

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

spec/helpers.rb
更好的解决方案 spec/spec_helper.rb
在Rails 4.2.0和rspec3.x中,这就是我的Rails\u 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

一个可能的答案是使用不同的方法:您可能必须在重新运行测试时删除它,这样它就不会被多次重新设定种子,使用类似database_cleaner的方法:
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
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