# Paperclip unit testing

**URL:** <https://www.ruby-forum.com/t/paperclip-unit-testing/171664>\
**Category:** Rails\
**Created:** [July 7, 2009, 11:59am UTC](https://www.ruby-forum.com/t/paperclip-unit-testing/171664 "2009-07-07T11:59:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [July 7, 2009, 11:59am UTC](https://www.ruby-forum.com/t/paperclip-unit-testing/171664/1 "2009-07-07T11:59:23Z")

</div>

Hi,

Rails 2.3.2, paperclip 2.2.9.2

How can I unit test my model that has a paperclip attachment?

I add to my model:

— CODE START —  
has\_attached\_file :image, :default\_url =\> ‘’  
attr\_protected :image\_file\_name, :image\_content\_type, :image\_image\_size  
— CODE END —

Than I type a UNIT test:

— CODE START —  
foo=FooModel.new({:image =\> File.new(‘test/fixtures/img/an\_image.gif’)})  
— CODE END —

This works fine until… in that model I have a validator that accesses  
the “image” attribute:

— CODE START —  
image.path  
— CODE END —

But there is no image under that path, since it points to  
RAILS\_ROOT/public/system/images/… What should I do now?

---

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [July 7, 2009, 1:03pm UTC](https://www.ruby-forum.com/t/paperclip-unit-testing/171664/2 "2009-07-07T13:03:48Z")

</div>

Yep, you actually need to upload the picture actually.

Take a look at the following code snippet I wrote some time back,  
for uploading image, resizing. (Ignore the code which is for image  
cropping)  
[129954’s gists · GitHub](http://gist.github.com/129954)

Thanks  
Srinivas

On Jul 7, 2:59 pm, Michal B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [July 7, 2009, 1:36pm UTC](https://www.ruby-forum.com/t/paperclip-unit-testing/171664/3 "2009-07-07T13:36:54Z")

</div>

Thanks!

How do you process the image in the model?

Before the save of the model object to database I want to add another  
image to it, that is a modified version of the “image” attribute.

I do:  
has\_attached\_file :image  
has\_attached\_file :image2  
after\_validation :prepare\_image2

And:  
def cprepare\_image2  
(…)  
path = image.queued\_for\_write[:original].path  
self.image2 = some\_rmagic\_processing(path)  
(…)  
end

Is there any other way to access the uploaded image?  
“image.queued\_for\_write[:original].path” does not look nice 😕

May be use an other callback?
