我试图通过定义 1 个工厂而不是 30 个来节省一些时间。
为什么这不起作用?
(假设我们有一个名为 :wanted_attributes 的类方法)
require 'rubygems'
require 'faker'
models = %w[Model1 Model2]
models.each do |model|
factoryname = model.downcase + "_e"
FactoryGirl.define do
factory factoryname.to_sym, :class => model do
model.constantize.wanted_attributes.each do |attribute|
attribute Faker::Name.first_name
end
end
end
end
我收到错误:
FactoryGirl::AttributeDefinitionError:属性已定义:属性