factory_bot / factory_girl 用于添加 belongs_to 的语法
factory_bot / factory_girl syntax for adding a belongs_to
我有以下型号:
class SecondaryIabCategory < ApplicationRecord
has_many :sites, foreign_key: :secondary_category_id
belongs_to :primary_iab_category
end
和以下有问题的工厂一起评论:
factory :primary_category, class: PrimaryIabCategory do
name 'Arts & Entertainment - yaddah'
value 'IAB1-some'
end
factory :another_primary_category, class: PrimaryIabCategory do
name 'Games are cool! - dabbah'
value 'IAB1-other'
end
factory :secondary_category, class: SecondaryIabCategory do
# here is the problem - what's the correct syntax?
primary_iab_category another_primary_category
name 'Test Secondary Cat'
value 'xxyy'
end
我收到错误消息:
NoMethodError: undefined method `another_primary_category=' for #<SecondaryIabCategory:0x00007f925c64e0d8>
我已经好几个月没有使用 FactoryBot/Girl 了,所以不确定它的语法是什么。有什么想法吗?
试试这个
factory :secondary_category, class: SecondaryIabCategory do
association :primary_iab_category, factory: :another_primary_category
name 'Test Secondary Cat'
value 'xxyy'
end
rubydoc很详细
我有以下型号:
class SecondaryIabCategory < ApplicationRecord
has_many :sites, foreign_key: :secondary_category_id
belongs_to :primary_iab_category
end
和以下有问题的工厂一起评论:
factory :primary_category, class: PrimaryIabCategory do
name 'Arts & Entertainment - yaddah'
value 'IAB1-some'
end
factory :another_primary_category, class: PrimaryIabCategory do
name 'Games are cool! - dabbah'
value 'IAB1-other'
end
factory :secondary_category, class: SecondaryIabCategory do
# here is the problem - what's the correct syntax?
primary_iab_category another_primary_category
name 'Test Secondary Cat'
value 'xxyy'
end
我收到错误消息:
NoMethodError: undefined method `another_primary_category=' for #<SecondaryIabCategory:0x00007f925c64e0d8>
我已经好几个月没有使用 FactoryBot/Girl 了,所以不确定它的语法是什么。有什么想法吗?
试试这个
factory :secondary_category, class: SecondaryIabCategory do
association :primary_iab_category, factory: :another_primary_category
name 'Test Secondary Cat'
value 'xxyy'
end
rubydoc很详细