I have (simplified) factories defined as follows:
factory :league do
acronym 'NBA'
end
factory :division do
league
end
Divisions belong to Leagues. When I define this factory, it was my assumption that 1 league would be created, and that league would be reused over and over again to give divisions a real league_id.
Instead, I'm getting errors on the 2nd call of FactoryGirl.create(:division)
because the League acronym
is supposed to be unique.
class League < ActiveRecord::Base
validates :acronym, uniqueness: true
end
leading to the following break in the test
ActiveRecord::RecordInvalid: Validation failed: Acronym has already been taken
How can I work around this, preferably without creating a hierarchy in the setup to the test?
If theres something better than factory_girl for what I'm trying to accomplish, please do suggest it