我尝试从底部列出的SO问题中获取解决方案,但我的问题是我正在使用Capybara和FactoryGirl,并且我似乎无法从任何地方加载seeds.rb,而不导致许多与种子数据完全分离的测试被破坏。
大多数错误消息都是以下内容的变体page.should_not have_content user.email
经过测试后,我尝试删除通过工厂创建的用户。在我加载种子数据之前,这些测试都顺利通过。
如何自动将 db:seed 数据加载到测试数据库中? https://stackoverflow.com/questions/1574797/how-to-load-dbseed-data-into-test-database-automatically
防止 Rails 测试删除种子数据 https://stackoverflow.com/questions/1898782/prevent-rails-test-from-deleting-seed-data
在 Rails 中播种数据库的最佳方法是什么? https://stackoverflow.com/questions/761123/what-is-the-best-way-to-seed-a-database-in-rails
如何在测试我的应用程序之前自动加载测试数据库中的数据? https://stackoverflow.com/questions/7459135/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: "[email protected] /cdn-cgi/l/email-protection",
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] /cdn-cgi/l/email-protection")
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