Skip to content

Stubbing calls to AWS in Rails tests

prathamesh profile image Prathamesh Sonpatki twitter logo github logo Updated on ・1 min read

RSpec Testing tips (3 Part Series)

1) Stubbing calls to AWS in Rails tests 2) RSpec let! and before order dependent behavior 3) follow_redirect! in Rails request specs

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)

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

Happy testing.

twitter logo DISCUSS
markdown guide
Classic DEV Post from Aug 13 '19

How open-source will Tumblr become?

In case you hadn't read the news, Verizon sold Tumblr to Automattic. Verizon owned Tumblr after it ac...

Prathamesh Sonpatki profile image
Prathamesh Sonpatki is consultant experienced in Ruby, Ruby on Rails.