mardi 4 avril 2017

Reuse Factory_Girl generated list in multiple scenarios in Capybara

I am using the FactoryGirl gem to generate a set of instances of my model for testing using capybara. Currently, the tests are checking to see if the filters work. The first test passes, however the second test does not.

This is the apply_filters.rb with the tests to run:

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

This is the Factory:

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

The second FactoryGirl.create_list(:scotland,9) however, generates a set of instances from MyString10 to MyString19. I want it to use the original MyString1 to MyString9. Ive tried global variables and placing the charities definition everywhere. What am i missing so that I can use charities in a series of upcoming tests?

Aucun commentaire:

Enregistrer un commentaire