# Testing acts\_as\_attachment?

**URL:** <https://www.ruby-forum.com/t/testing-acts-as-attachment/87979>\
**Category:** Rails\
**Created:** [March 7, 2007, 10:06am UTC](https://www.ruby-forum.com/t/testing-acts-as-attachment/87979 "2007-03-07T10:06:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 7, 2007, 10:06am UTC](https://www.ruby-forum.com/t/testing-acts-as-attachment/87979/1 "2007-03-07T10:06:04Z")

</div>

Since attachment\_fu isnâ€™t working for me at the moment, Iâ€™ve switched to  
acts\_as\_attachment.

Does anyone know how to set up a simple unit test for  
acts\_as\_attachment? Iâ€™ve ventured into the included tests but theyâ€™re a  
bit over my head, to be honest.

Is it possible to use fixtures to test aaa?

For example, suppose I have a simple model called Picture, which has  
just the basic acts\_as\_attachment fields. How would I set up some simple  
unit tests to make sure itâ€™s working? Can I use fixtures or do I need to  
use methods in my unit tests to use fixture\_file\_upload?

Iâ€™m wondering if anyone has any straightforward examples for a very  
simple case that I could learn from. Thanks!

Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 11, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/testing-acts-as-attachment/87979/2 "2007-03-11T17:45:13Z")

</div>

Hi,

I found this test helper on the Rails wiki, works like a charm.

# returns an uploaded screenshot you can use to test file uploads in

your controllers  
def uploaded\_screenshot  
uploaded\_file(“#{File.expand\_path(RAILS\_ROOT)}/test/fixtures/  
samplescreenshot.jpg”, “image/jpeg”)  
end

# returns an uploaded movie you can use to test file uploads in your

controllers  
def uploaded\_movie  
uploaded\_file(“#{File.expand\_path(RAILS\_ROOT)}/test/fixtures/  
samplemovie.flv”, “video/flv”)  
end

# get us an object that represents an uploaded file

def uploaded\_file(path, content\_type=“application/octet-stream”,  
filename=nil)  
filename ||= File.basename(path)  
t = Tempfile.new(filename)  
FileUtils.copy\_file(path, t.path)  
(class \<\< t; self; end;).class\_eval do  
alias local\_path path  
define\_method(:original\_filename) { filename }  
define\_method(:content\_type) { content\_type }  
end  
return t  
end

put that in test\_helper.rb

Then in your controller test, put this:

# update a fragment

# uses test helper uploaded\_screenshot() to simulate a file upload

def test\_should\_update\_fragment  
put :update, :id =\> 1, :fragment =\> { :title =\> ‘new f’ },  
:screenshot =\> { :uploaded\_data  
=\> uploaded\_screenshot }

```
assert_redirected_to fragment_path(assigns(:fragment)),

```

assigns(:fragment).errors.inspect  
end

On Mar 7, 10:06 am, Jeff C.man [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
