factory girl uniqueness validation fails for associated factories

半世苍凉 提交于 2019-12-04 00:38:15

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.

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.

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