我正在使用FactoryGirl gem生成一组模型实例,以便使用capybara进行测试。目前,测试正在检查过滤器是否有效。第一次测试通过,但第二次测试没有。
这是apply_filters.rb,其中包含要运行的测试:
describe "applying filters to the scotland list" do
scenario "i apply a minimum income filter" do
charities = FactoryGirl.create_list(:scotland, 9)
visit "/scotsfilter"
fill_in('Minimum Income', :with => 5)
click_button('Apply Filters')
expect(page).to have_no_content(charities[0].Charity_Number)
expect(page).to have_content(charities[5].Charity_Number)
expect(page).to have_content(charities[6].Charity_Number)
end
scenario "i apply a maximum income filter" do
charities = FactoryGirl.create_list(:scotland, 9)
visit "/scotsfilter"
fill_in('Maximum Income', :with => 5)
click_button('Apply Filters')
expect(page).to have_content(charities[0].Charity_Number)
expect(page).to have_no_content(charities[5].Charity_Number)
expect(page).to have_no_content(charities[6].Charity_Number)
end
end
这是工厂:
FactoryGirl.define do
factory :scotland do
sequence(:id) {|n| n}
sequence(:Charity_Number){|n| "MyString#{n} "}
Charity_Name "MyString"
Registered_Date "MyString"
Known_As "MyString"
Charity_Status "MyString"
Postcode "MyString"
Constitutional_Form "MyString"
Previous_Constitutional_Form_1 "MyString"
Geographical_Spread "MyString"
Main_Operating_Location "MyString"
sequence(:Purposes){|n| "MyString#{n}"}
sequence(:Beneficiaries){|n| "MyString#{n}"}
Activities "MyString"
Objectives "MyString"
Principal_Office_Trustees_Address "MyString"
Website "MyString"
sequence(:Most_recent_year_income){|n| n}
sequence(:Most_recent_year_expenditure){|n| n}
Mailing_cycle "MyString"
Year_end "MyString"
Parent_charity_name "MyString"
Parent_charity_number "MyString"
Parent_charity_country_of_registration "MyString"
Designated_religious_body "MyString"
Regulatory_type "MyString"
end
end
然而,第二个FactoryGirl.create_list(:scotland,9)
会生成一组从MyString10
到MyString19
的实例。我希望它使用原始MyString1
到MyString9
。我已经尝试过全局变量并将charities
定义放在任何地方。我缺少什么才能在即将进行的一系列测试中使用charities
?
答案 0 :(得分:0)
您不能在多个测试中重复使用相同的对象,在每个测试之间清除数据库,专门用于将一个测试与另一个测试隔离。另外,如果创建的对象的特定值对于给定的测试很重要,则应在创建对象时指定该值,并让工厂处理无关紧要的值
因此,在您的示例测试中,您可能需要类似
的内容charities = (1..9).map {|income| FactoryGirl.create(:scotland, Most_recent_year_income: income }