Ruby on rails 如何让工厂女孩与协会合作?
以前我有一个工厂,看起来像这样:Ruby on rails 如何让工厂女孩与协会合作?,ruby-on-rails,rspec,factory-bot,rspec-rails,Ruby On Rails,Rspec,Factory Bot,Rspec Rails,以前我有一个工厂,看起来像这样: FactoryGirl.define do factory :file do ignore do attachment_id 1 end trait :file_with_autoplay do type_key "file_with_autoplay" attrs {{ "path" => attachment_id, "volume" => 1
FactoryGirl.define do
factory :file do
ignore do
attachment_id 1
end
trait :file_with_autoplay do
type_key "file_with_autoplay"
attrs {{
"path" => attachment_id,
"volume" => 1
}}
end
end
end
当我用
create(:file, :file_with_autoplay, { story: story, attachment_id: attachment.id } )
而且效果很好
现在我必须稍微重构模型。在故事之前
有文件
和属性
列。现在文件
有一对一的模式配置
,在这里我提取了类型_键
和属性
所以,我的工厂是这样的:
FactoryGirl.define do
factory :file do
ignore do
attachment_id nil
end
trait :file_with_autoplay do
association :config,
configuration_type: "file",
type_key: "file_with_autoplay",
attrs: {
"path" => "#{attachment_id}",
"volume" => 1
}
end
end
end
所以,没有太大的变化,但我现在将附件id传递给协会。
当我运行测试时,我得到
ArgumentError:
Trait not registered: attachment_id
为清楚起见,这是我新的config
工厂:
FactoryGirl.define do
factory :config do
ignore do
attachment_id nil
end
configuration_type "file"
type_key "file_with_autoplay"
attrs {{
"generic_attr" => "generic_value"
}}
end
end
我做错了什么?我应该如何通过Factory Girl中的关联传递附件\u id?@ArupRakshit,那么我看不出有错误。但测试失败,因为我验证了路径是否存在。使用大括号表示语法错误。仅使用
“path=>attachment\u id.to s,
仍然Trait未注册:attachment\u id
语法错误,意外“}”,预期=>
让我们来看看。