# Mocking Plugins

**URL:** <https://www.ruby-forum.com/t/mocking-plugins/137835>\
**Category:** Rails\
**Created:** [May 16, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/mocking-plugins/137835 "2008-05-16T19:51:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [May 16, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/mocking-plugins/137835/1 "2008-05-16T19:51:34Z")

</div>

I’m using rspec on a current rails project and one of the plugins I’m  
using (Paperclip) uploads file attachments for a model to S3. This  
obviously slows the tests down somewhat, and I would like to remove this  
altogether by stubbing – the question is what should I be stubbing?

Should I stub the upload method in the plugin class or do it in the  
model class?

Any advice?

Thanks,

~ Mark

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [May 16, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/mocking-plugins/137835/2 "2008-05-16T20:48:26Z")

</div>

Mark D. wrote:

> I’m using rspec on a current rails project and one of the plugins I’m  
> using (Paperclip) uploads file attachments for a model to S3. This  
> obviously slows the tests down somewhat, and I would like to remove this  
> altogether by stubbing – the question is what should I be stubbing?
> 
> Should I stub the upload method in the plugin class or do it in the  
> model class?

Whip out Mocha (or the other leading mocker), and stub the lowest call  
in your  
own code; the first call into the library code.

(This is generic advice how to mock!)

We do it like this:

def toast\_hit\_real\_server  
obj = assemble  
obj.activate # hits a server  
p obj.response  
end

def test\_hit\_mock\_server  
obj = assemble  
Paperclip.any\_instance.expects(:upload).returns(:I\_Likes)  
obj.activate # hits the mock  
assert{ obj.response == :I\_Likes }  
end

Write the first test, and when you run your test batch it hits the real  
server,  
and you can debug it and manually inspect the output.

Then you “toast” the first test, and write the second test case to  
exactly match  
the first, with the mocker installed.

Then we refactor the mocker into a reusable test method, such as  
mock\_paperclip.

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [May 16, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/mocking-plugins/137835/3 "2008-05-16T22:10:48Z")

</div>

This is Phlips area! Phlip did you pay him to ask this???

No real need to roll to Mocha, though, except for preference. rspec’s  
mocking capabilities are pretty similar (though I personally find it’s  
syntax much prettier).
