假设我有一个城市模型,其中:
class city
field :full_name, type: String # San Francisco, CA, United States
field :_id, type: String, overwrite: true, default: ->{ full_name }
end
假设我在/spec/factories/cities.rb中定义了一个工厂:
FactoryGirl.define do
factory :city do
full_name 'San Francisco, CA, United States'
end
end
在其中一个规范中运行以下代码:
city_attrs = { full_name: 'San Francisco, CA, United States' }
City.create! city_attrs
=> #<City _id: San Francisco, CA, United States, full_name: "San Francisco, CA, United States">
FactoryGirl.create(:city)
=> #<City _id: , full_name: "San Francisco, CA, United States">
如何在不将以下代码添加到/spec/factories/cities.rb的情况下解决此问题?
before(:create) do |city, evaluator|
city.id = city.full_name
end
编辑解决方案是按照此答案中的建议
停止使用FactoryGirl
并改用Fabrication