在水豚测试中使用导轨模型
问题描述:
我建立了一个项目,我所要做的就是显示一组来自SQLlite3数据库的记录。我在该数据库中有一个表格,我希望根页面显示数据库中的所有记录。我有这个工作,但现在我想建立一个水豚测试,以确保页面上的第一个和最后一个记录从页面上的表。在水豚测试中使用导轨模型
require 'rails_helper'
describe "seeing record from scotlands model " do
specify "I can see a list of all charities" do
visit "/"
expect(page).to have_content "@table.first_record"
end
end
但是,上面没有提供链接到模型,所以我无法访问它。如何从测试文件中获取表格的链接?
答
您是否通常尝试从测试中访问真实数据?我一直学会将这些事情分开。
我喜欢与Rspec和Capybara合作。这里是简单而直接的,应该完成你所讨论的内容:
require 'rails_helper'
feature "user sees all scotlands records" do
scenario "successfully" do
charity1 = Charity.create(name: name1, info: info1)
charity2 = Charity.create(name: name2, info: info2)
charity3 = Charity.create(name: name3, info: info3)
visit root_path
expect(page).to have_content(charity1.name)
expect(page).to have_content(charity1.info)
expect(page).to have_content(charity2.name)
expect(page).to have_content(charity2.info)
expect(page).to have_content(charity3.name)
expect(page).to have_content(charity3.info)
end
end
我其实通常也和FactoryGirl一起工作。在这种情况下,它会让事情变得更加简单,因为您可以使用create_list
并只需一行代码就可以创建尽可能多的记录。
我打算使用实际数据,但它似乎不是正确的做法。 FactoryGirl的建议听起来不错,我会研究一下。谢谢。 –
是的,通常Rails不会触及测试环境中的真实分贝。 – vivipoit