Mocking Plugins

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

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.

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).