factory girl uniqueness validation fails for assoc

2019-04-04 18:12发布

问题:

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

回答1:

Use initialize_with in your league definition.

See http://robots.thoughtbot.com/post/16196616388/factory-girl-2-5-gets-custom-constructors

You could then issue find_or_create_by_acronym to guarantee it's created once.



回答2:

Depending on where you're calling FactoryGirl.create the records will get created for every spec you have. What you want is database_cleaner, once set up, it will clean up your database after every spec making sure your validation errors are no longer an issue.

EDIT

Whoops, I misread your question. What you'll want to do is either use the faker gem to generate random strings for each acronym or use factory_girl sequence like this

FactoryGirl.define do
  sequence :acronym do |n|
    "NBA#{n}"
  end

  factory :league do
    acronym
  end
end

Using sequence will actually make sure that every league created has a unique acronym.