如何在不破坏 FactoryGirl 的情况下将 Seeds.rb 加载到测试数据库中? [英] How can I load seeds.rb into the test database without breaking FactoryGirl?

查看:56
本文介绍了如何在不破坏 FactoryGirl 的情况下将 Seeds.rb 加载到测试数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

如何自动加载 db:seed 数据到测试数据库?

防止 Rails 测试删除种子数据

什么是最好的播种方式Rails 中的数据库?

如何在测试我的应用程序之前自动加载测试数据库中的数据?

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

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: "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之前通过

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

推荐答案

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

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


更好的解决方案

spec/spec_helper.rb

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

这篇关于如何在不破坏 FactoryGirl 的情况下将 Seeds.rb 加载到测试数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆