【发布时间】:2021-10-19 17:08:34
【问题描述】:
spec/factories/organisation_groups.rb
FactoryBot.define do
@organisation_groups = [{name: 'WWZY', uuid: '68a0c637'}, {name: 'WCC', uuid: '0a13362e'}, {name: 'ECW', uuid: 'fdcdb1b2'}, {name: 'ZPE', uuid: 'fdcdb1b8'}]
@organisation_groups.each do |group|
factory :organisation_group do
name group[:name]
uuid group[:uuid]
end
end
end
我尝试使用 FactoryBot 创建多个记录,但问题是我收到此错误:
NoMethodError:
undefined method 'name' in 'organisation_group' factory
Did you mean? 'name { "WWZY" }'
user_spec.rb
describe '#organisation_group' do
before(:each) do
@organisation_groups = create(:organisation_group)
end
end
更新 1
FactoryBot::DuplicateDefinitionError:
Factory already registered: organisation_group
【问题讨论】:
-
在循环内部的工厂内部,应该是
name { group[:name] } -
@LesNightingill 如果我按照您的建议进行操作,那么我会收到问题更新 1 中所述的错误。
-
哦,是的,当然,我错过了那个错误,请参阅下面的答案
标签: ruby-on-rails ruby testing rspec