# Testing with attachment\_fu

**URL:** <https://www.ruby-forum.com/t/testing-with-attachment-fu/103201>\
**Category:** Rails\
**Created:** [May 2, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/testing-with-attachment-fu/103201 "2007-05-02T21:58:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dissolved](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b3f665/32.png) [@dissolved](https://www.ruby-forum.com/u/dissolved)\
**Post date:** [May 2, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/testing-with-attachment-fu/103201/1 "2007-05-02T21:58:12Z")

</div>

Mike C.'s attachment\_fu guide is great! ([http://clarkware.com/cgi/](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

---

<div class="post-metadata">

**Author:** ![dissolved](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b3f665/32.png) [@dissolved](https://www.ruby-forum.com/u/dissolved)\
**Post date:** [May 11, 2007, 12:25am UTC](https://www.ruby-forum.com/t/testing-with-attachment-fu/103201/2 "2007-05-11T00:25:34Z")

</div>

Ryan S. wrote:

> Mike C.'s attachment\_fu guide is great! ([http://clarkware.com/cgi/](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
