Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

  • Blog Navigation
Mocking Fog when using it with Carrierwave

There is a new kid on the block when it comes to file attachments for Rails and it is called Carrierwave.

Carrierwave gives you the ability to easily store attachments on S3 using another great gem called Fog.

Uploading files to S3 is great for many reasons but it can slow down your testing environment because it takes a while to send stuff up to S3. The Carrierwave documentation tells you how to switch the storage location over to file storage during testing but that wasn’t enough for me. I wanted to use the same storage mechanism for dev, test and production so I sought out a way to do so.

I had heard about Fog’s ability to mock itself to pretend that it was interacting with S3 so I decided to see if I could get it working with Carrierwave. This allowed me to use the same storage mechanism in test mode without slowing my tests down waiting for images to go to S3.

After a bunch of tinkering and a message on the Fog mailing list(thanks for the quick response Wesley), this is what I came up with:

The key is that you have to tell the mocked Fog that an S3 bucket exists before it will let Carrierwave put an image there. I wasn’t doing this at first and Carrierwave kept showing me a 404 error from Fog.

Drop this in a file in your spec/support and/or features/support
directories and you will have your tests thinking they are sending things to S3 without actually sending them to S3.

Now I don’t have to mess around with having a bunch of test images laying around my hard drive and I can make sure I’m using the same storage mechanism across all environments without slowing my tests down.

Your mileage may vary but I’d love to hear how this works for people and if there are any limitations. I haven’t found any yet.

Check out my guest post on the Engine Yard Blog for updated details:

  1. Dr Nic says:

    Thanks for the article. If you are setting up your entire fog/carrierwave configuration in your cucumber environment, what initialization do you have for you main rails app (config/initializers/fog.rb or carrierwave.rb?)


  2. Mike Gehard says:

    Hey there Dr. Nick,

    Sorry I’ve been a bit AWOL on this. Traveling for work…

    Do you still need an answer? I am working on a guest blog post to the ey site this weekend that should help answer this question. Stay tuned. If it doesn’t answer the question then we can circle back up then.


  3. Mike Gehard says:

    Dr. Nick,

    Blog post written and submitted on the Engine Yard blog site. Publish date soon hopefully. Bet you could get a sneak peek if you wanted to.


  4. Jesse Cooke says:

    I would LOVE to see the new blog post Mike. We’re a day out from deploying carrierwave to EY & mucking around with different storage mechanisms is no bueno.

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *

Share This