You can use fixture_file_upload
include ActionDispatch::TestProcess
in your test helper, here is an example factory:
include ActionDispatch::TestProcess
FactoryBot.define do
factory :user do
avatar { fixture_file_upload(Rails.root.join('spec', 'photos', 'test.png'), 'image/png') }
end
end
In the above example, spec/photos/test.png
needs to exist in your application's root directory before running your tests.
Note, that FactoryBot
is a new name for FactoryGirl
.
Newer FG syntax and no includes necessary
factory :user do
avatar { File.new(Rails.root.join('app', 'assets', 'images', 'rails.png')) }
end
or, better yet,
factory :user do
avatar { File.new("#{Rails.root}/spec/support/fixtures/image.jpg") }
end
Desmond Bowe over at Pivotal Labs suggests avoiding fixture_file_upload due to memory leak problems. Instead, you should set the paperclip fields directly in your factory:
factory :attachment do
supporting_documentation_file_name { 'test.pdf' }
supporting_documentation_content_type { 'application/pdf' }
supporting_documentation_file_size { 1024 }
# ...
end
I've been using the code in the gist below:
Rails 2
http://gist.github.com/162881
Rails 3
https://gist.github.com/313121
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With