Testing with attachment_fu

Mike C.'s attachment_fu guide is great! (http://clarkware.com/cgi/
blosxom/2007/02/24) But what about unit and functional testing? Does
anyone have some examples they can share? I seem to be getting stuck
on how to send upload data to my attachment models via a test file.
Thanks!

– RYAN

Ryan S. wrote:

Mike C.'s attachment_fu guide is great! (http://clarkware.com/cgi/
blosxom/2007/02/24) But what about unit and functional testing? Does
anyone have some examples they can share? I seem to be getting stuck
on how to send upload data to my attachment models via a test file.
Thanks!

– RYAN

I’ve been having that problem this evening too. I solved it by using the
fixture_file_upload helper to upload my test data. Put the picture into
test/fixtures and then use something like:

new_photo.uploaded_data = fixture_file_upload(‘test_photo.jpg’,
‘image/jpeg’))
assert new_photo.save

I also created a method in test_helper.rb to update my fixtures with an
attachment at the start of every test. I can now call this from setup on
any of my unit tests along with fixtures :photos

def photo_fixtures
photos = Photo.find(:all)
photos.each do |photo|
photo.uploaded_data = fixture_file_upload(photo.filename,
‘image/jpeg’)
photo.save!
end
end

I’m new to rails and unit testing so I don’t know if there is a more
elegant solution for this. Probably.

There are tons of tests written in the test folder of the plugin too but
I couldn’t find any guidance on adapting them for your own use so
decided to start from scratch. As I add more tests I’ll borrow from
there I guess.

Mike