我有一家卡工厂(定义如下),
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
答案 0 :(得分:1)
由于card
与many-many
tags
关联
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
答案 1 :(得分:1)
好的,您在tags
和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关联,你可以这样做:
class Test
{
async Task IndexAsync()
{
var nottheactualtype = GetType(); //This references the "Test" class, but this operation is actually located in the nested state machine class named "IndexAsync", in the method "MoveNext()".
var actualcalledmethod = new StackTrace().GetFrame(0).GetMethod(); //This shows the actual method currently being run: IndexAsync.MoveNext().
//But how do I get the reference to my current IndexAsync class?
}
}
它是&#34;轻量级&#34;在工厂中建立HABTM的方法。我已经提到过&#34;彻底&#34;在我最近的答案的one中的方式。
答案 2 :(得分:0)
我已通过将工厂代码更改为以下代码来解决问题,
FactoryGirl.define do
factory :card do
front { Faker::Lorem.sentence }
back { Faker::Lorem.sentence }
# associations
user
tag
meta_sm2
action_record
end
end
我认为之前的问题与缺失的关联有关。