【问题标题】:How can I load seeds.rb into the test database without breaking FactoryGirl?如何在不破坏 FactoryGirl 的情况下将 seed.rb 加载到测试数据库中?
【发布时间】:2012-04-09 07:18:10
【问题描述】:

我已尝试从底部列出的 SO 问题中获取解决方案,但我的问题是我正在使用 Capybara 和 FactoryGirl,而且我似乎无法从任何地方加载 seed.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?


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

种子.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

测试失败

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

【问题讨论】:

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


【解决方案1】:

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

spec/helpers.rb

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

更好的解决方案

spec/spec_helper.rb

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

【讨论】:

【解决方案2】:

Rails 4.2.0RSpec 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 2012-04-28
    • 2010-09-07
    相关资源
    最近更新 更多