我正在寻找一种为具有活动存储附件的模型创建工厂的方法
我尝试了this post
中的方法与我的工厂一起
factory :activity_fit_file, class: 'Activity' do
association :user, factory: :user
activity_type {:cycling}
original_activity_log_file { fixture_file_upload("#{Rails.root}/spec/files/example_fit_file.fit") }
end
但是我遇到了这个错误
NoMethodError:
undefined method `fixture_file_upload' for #<FactoryBot::SyntaxRunner:0x000000000208c5f8>
在ActiveStorage中附加文件的正确方法是什么?
答案 0 :(得分:1)
此解决方案适用于 rails 6:
factory :post do
# ...
after(:build) do |post|
post.image.attach(
io: File.open(Rails.root.join('test', 'fixture_files', 'test.jpg')),
filename: 'test.jpg',
content_type: 'image/jpeg'
)
end
end
答案 1 :(得分:0)
直接尝试Rack::Test::UploadedFile
:
file { Rack::Test::UploadedFile.new('path', 'image/png') }
答案 2 :(得分:0)
将代码更改为以下代码即可。
include ActionDispatch::TestProcess
factory :activity_fit_file, class: 'Activity' do
association :user, factory: :user
activity_type {:cycling}
original_activity_log_file { fixture_file_upload("#{Rails.root}/spec/files/example_fit_file.fit") }
end