DEV Community

Prathamesh Sonpatki
Prathamesh Sonpatki

Posted on • Edited on

9 2

Stubbing calls to AWS in Rails tests

I use aws-sdk gem to deal with file uploads to S3 in my Rails application. I needed to add tests for the account logo upload and removal feature. I use Paperclip gem to do the file uploads.

Typically, applications use file storage in test environment to avoid hitting AWS API but in this application I was constrained to not use file storage in test environment.

Obviously I did not want to hit the AWS API during tests. I came up with a solution using a technique provided by aws-sdk gem.

Just drop following line of code in test_helper or rails_helper.

# Stub all calls to AWS
  config.before do
    Aws.config.update(stub_responses: true)
  end
Enter fullscreen mode Exit fullscreen mode

And you are good. All API calls to AWS are now stubbed!

Happy testing.

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay