Recent Projects

Stubbing out S3 calls in Paperclip

Here’s a quick tip for people using Paperclip that I mentioned on the Google Group. You can easily stub out calls to S3 in your tests using mocha like so:

Photo.any_instance.stubs(:save_attached_files).returns(true)
Photo.any_instance.stubs(:destroy_attached_files).returns(true)

Of course, you’d want to change the Photo bit depending on the model you have using Paperclip.

Hopefully this little tip will save others the fruitless Googling and annoying code digging I did :)

5 Responses to “Stubbing out S3 calls in Paperclip”

  1. Thanks, I was stubbing one instance a time with save_attached_files, but here you are using any_instance, which is a clever method.

  2. Ben Scofield says:

    You may also want to stub the :delete_attached_files method, as well. That one bit me earlier.

  3. Trevor says:

    Nice on, Ben. Thanks!

  4. Mike says:

    :delete_attached_files didn’t work for me, but :destroy_attached_files did.

  5. Trevor says:

    Thanks, Mike – updated.

Leave a Reply