There is some test functionality provided with paperclip, but it is
for shoulda and I have not really pursued this direction yet.
Instead I have taken the following approach. I am using Machinist for
random test data. You could simplify this to use a single test image
if you prefer.
Put some images into spec/fixtures directory with a common name, e.g.
example1.jpg example2.gif example3.jpg and so on
My paperclip binding is called picture and it's on my service object.
This is in my service steps file:
def new_picture
@new_picture ||= Sham.image
end
When /^I provide an image file for "picture"$/ do
attach_file "service_picture", new_picture.path
end
And in my Sham.define block (in spec/blueprints.rb)
image do
images = []
dirpath = File.join Rails.root, 'spec', 'fixtures'
Dir.new(dirpath).each { |file| images << File.new(File.join
(dirpath, file)) if file =~ /^example\d/ }
images[rand(images.length)]
end
The gotcha is cleaning up the files afterwards, since we're not using
the database for a file store. I took this approach (which is hacky
but works, on unix) to put the paperclip test files into a different
directory and delete the directory afterwards.
features/support.env
#remove paperclip files
module Paperclip::Interpolations
alias_method :org_attachment, :attachment
def attachment(att, style)
"CUKE/" + org_attachment(att, style)
end
end
After do
`rm -rf #{"#{RAILS_ROOT}/public/system/CUKE"}`
end
#end remove paperclip files
Hope this helps. If it doesn't work I may have forgotten some code.
Regards
Nick