Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock aws-sdk gem?

I have some code that uploads a file to Amazon S3, using the aws-sdk gem. Apparently it does an HTTP put to upload the file.

Is there a good way to mock this functionality of the aws-sdk gem?

I tried using Webmock, but the aws-sdk gem seems to do a get latest/meta-data/iam/security-credentials/ first. It seems that using Webmock may not be the best way to mock this functionality.

Working in RSpec.

like image 999
B Seven Avatar asked May 05 '13 01:05

B Seven


2 Answers

If you're using version 2 of the aws-sdk gem try adding:

Aws.config.update(stub_responses: true)

to your RSpec.configure block (usually found in your rails_helper.rb file)


While the above works, it will return empty responses if you don't further specify response content - not necessarily valid, but stubbed.

You can generate and return stubbed response data from a named operation:

s3 = Aws::S3::Client.new
s3.stub_data(:list_buckets)
#=> #<struct Aws::S3::Types::ListBucketsOutput buckets=[], owner=#<struct Aws::S3::Types::Owner display_name="DisplayName", id="ID">>

In addition to generating default stubs, you can provide data to apply to the response stub.

s3.stub_data(:list_buckets, buckets:[{name:'aws-sdk'}])
#=> #<struct Aws::S3::Types::ListBucketsOutput buckets=[#<struct Aws::S3::Types::Bucket name="aws-sdk", creation_date=nil>], owner=#<struct Aws::S3::Types::Owner display_name="DisplayName", id="ID">>

For more details refer to: http://docs.aws.amazon.com/sdkforruby/api/Aws/ClientStubs.html

like image 65
Mark Murphy Avatar answered Oct 01 '22 16:10

Mark Murphy


There are a lot of ways to mock requests in the AWS SDK for Ruby. Trevor Rowe recently posted an article on using the SDK's native support for object stubbing, which does not require any external dependencies like Webmock. You can also use tools like VCR (link will send you to another blog post) to build cacheable integration tests; this way you can test against the live service when you want accuracy and avoid hitting network when you want speed.

Regarding the get request on latest/meta-data/iam/security-credentials/, this happens because the SDK is trying to look up credentials, and, if none are provided, it will check if you are running on an EC2 instance as a last resort, causing the SDK to make an extra HTTP request. You can avoid this check by simply providing bogus static credentials, though if you are using something like VCR, you will want to provide valid credentials for the first run. You can read about how to provide static credentials in another blog post that Trevor wrote on credential management (this should also be in the developer guide and SDK documentation).

like image 39
Loren Segal Avatar answered Oct 01 '22 16:10

Loren Segal