我想知道FactoryGirl中是否有find_or_initialize_by
的等价物来解决以下问题:
目标是该模型使用两个具有相同国家/地区的表格。我不想在国家使用序列(正如我在电子邮件中找到的那样)。
Country上有一个唯一性约束,但我的主要问题是,当我调用FactoryGirl.create(:click)时,它会创建两次相同的Country记录
因此,验证在测试中失败。
Rspec的:
# models/click_spec.rb
describe Click do
it "should have a valid constructor" do
FactoryGirl.create(:click).should be_valid
end
end
工厂:
# factories/countries.rb
FactoryGirl.define do
factory :country do
name "United States"
slug "us"
end
end
# factories/offers.rb
FactoryGirl.define do
factory :offer do
association :country, factory: :country
# Other columns
end
end
# factories/users.rb
FactoryGirl.define do
factory :user do
association :country, factory: :country
# Other columns
end
end
# factories/clicks.rb
FactoryGirl.define do
factory :click do
association :offer, factory: :offer
association :user, factory: :user
# Other columns
end
end
型号:
class Country < ActiveRecord::Base
validates :name, :slug,
presence: true,
uniqueness: { case_sensitive: false }
validates :slug,
length: { is: 2 }
end
答案 0 :(得分:3)
您应该可以使用initialize_with
:
FactoryGirl.define do
factory :country do
name "United States"
slug "us"
initialize_with { Country.find_or_create_by_name(name) }
end
end
这将始终使用相同的国家/地区。您可能希望嵌套工厂以允许其他工厂使用不同的名称:
FactoryGirl.define do
factory :country do
initialize_with { Country.find_or_create_by_name(name) }
factory :united_states do
name "United States"
slug "us"
end
end
end
答案 1 :(得分:0)
我的应用程序Country
模型也遇到了类似的问题。这就是我所做的。
为确保FactoryBot的build
和create
仍能正常工作,我们应该通过执行以下操作来仅覆盖to_create
的逻辑:
factory :country do
to_create do |instance|
instance.attributes = Country.create_with(name: instance.name).find_or_create_by(slug: instance.slug).attributes
instance.reload
end
name { "United States" }
slug { "us" }
end
查询说明:
Country
.create_with(name: instance.name) # if not found, create with this `name` (and `slug` defined below)
.find_or_create_by(slug: instance.slug) # find by primary key `slug'
这可确保build
保持其默认的“构建/初始化对象”行为,并且不执行任何数据库读取或写入操作,因此始终保持快速状态。仅覆盖create
的逻辑以获取现有记录(如果存在),而不是尝试始终创建新记录。
最初发布于https://stackoverflow.com/a/55235861/3956879。
查看我的article对此进行解释。