FactoryGirl InvalidFactoryError NoMethodError
FactoryGirl InvalidFactoryError NoMethodError
我有一个卡片工厂(定义如下),
FactoryGirl.define do
factory :card do
front { Faker::Lorem.sentence }
back { Faker::Lorem.sentence }
tags { Faker::Lorem.words(3).join(';') }
# associations
user
tag
end
end
当我尝试 运行 规范时,出现以下错误,
An error occurred in a `before(:suite)` hook.
Failure/Error: FactoryGirl.lint
FactoryGirl::InvalidFactoryError:
The following factories are invalid:
* card - undefined method `each' for "eum;et;ad":String (NoMethodError)
我不知道为什么会这样,下面的代码是我的卡片模型,
class Card < ApplicationRecord
validates :front, presence: true
belongs_to :user
has_one :meta_sm2
has_many :action_records
has_and_belongs_to_many :tags
delegate :username, :email, :name,
to: :user, prefix: true
end
因为 card
与 tags
有 many-many
关联
tags
期望一个 array
,而不是你传递了一个字符串,
试试,用tags { Faker::Lorem.words(3) }
代替tags { Faker::Lorem.words(3).join(';') }
FactoryGirl.define do
factory :card do
front { Faker::Lorem.sentence }
back { Faker::Lorem.sentence }
tags { Faker::Lorem.words(3) }
# associations
user
tag
end
end
我已经通过将工厂代码更改为以下代码解决了这个问题,
FactoryGirl.define do
factory :card do
front { Faker::Lorem.sentence }
back { Faker::Lorem.sentence }
# associations
user
tag
meta_sm2
action_record
end
end
我认为上一个问题与缺失关联有关。
好的,您在 cards
和 tags
之间建立了 HABTM 关联。我真的不明白你怎么能只用一个数组或一个字符串而不是关联的对象来设置 tags
但我仍然相信你想在你的工厂中建立正确的 HABTM 关联并且可以这样做:
FactoryGirl.define do
factory :card do
front { Faker::Lorem.sentence }
back { Faker::Lorem.sentence }
# tags { Faker::Lorem.words(3).join(';') }
# associations
user
factory :card_with_tags do
after(:create) do |book|
create_list(:tag, 3, cards: [card])
end
end
end
end
这是在工厂中设置 HABTM 的 "lighweight" 方法。我在最近的回答 one 中提到了 "thorough" 方式。
我有一个卡片工厂(定义如下),
FactoryGirl.define do
factory :card do
front { Faker::Lorem.sentence }
back { Faker::Lorem.sentence }
tags { Faker::Lorem.words(3).join(';') }
# associations
user
tag
end
end
当我尝试 运行 规范时,出现以下错误,
An error occurred in a `before(:suite)` hook.
Failure/Error: FactoryGirl.lint
FactoryGirl::InvalidFactoryError:
The following factories are invalid:
* card - undefined method `each' for "eum;et;ad":String (NoMethodError)
我不知道为什么会这样,下面的代码是我的卡片模型,
class Card < ApplicationRecord
validates :front, presence: true
belongs_to :user
has_one :meta_sm2
has_many :action_records
has_and_belongs_to_many :tags
delegate :username, :email, :name,
to: :user, prefix: true
end
因为 card
与 tags
many-many
关联
tags
期望一个 array
,而不是你传递了一个字符串,
试试,用tags { Faker::Lorem.words(3) }
代替tags { Faker::Lorem.words(3).join(';') }
FactoryGirl.define do
factory :card do
front { Faker::Lorem.sentence }
back { Faker::Lorem.sentence }
tags { Faker::Lorem.words(3) }
# associations
user
tag
end
end
我已经通过将工厂代码更改为以下代码解决了这个问题,
FactoryGirl.define do
factory :card do
front { Faker::Lorem.sentence }
back { Faker::Lorem.sentence }
# associations
user
tag
meta_sm2
action_record
end
end
我认为上一个问题与缺失关联有关。
好的,您在 cards
和 tags
之间建立了 HABTM 关联。我真的不明白你怎么能只用一个数组或一个字符串而不是关联的对象来设置 tags
但我仍然相信你想在你的工厂中建立正确的 HABTM 关联并且可以这样做:
FactoryGirl.define do
factory :card do
front { Faker::Lorem.sentence }
back { Faker::Lorem.sentence }
# tags { Faker::Lorem.words(3).join(';') }
# associations
user
factory :card_with_tags do
after(:create) do |book|
create_list(:tag, 3, cards: [card])
end
end
end
end
这是在工厂中设置 HABTM 的 "lighweight" 方法。我在最近的回答 one 中提到了 "thorough" 方式。