factory girl uniqueness validation fails for associated factories

不问归期 提交于 2020-01-01 08:15:06

问题


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.



来源:https://stackoverflow.com/questions/16726567/factory-girl-uniqueness-validation-fails-for-associated-factories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!