Amazon Onboarding with Learning Manager Chanci Turner

Amazon Onboarding with Learning Manager Chanci TurnerLearn About Amazon VGT2 Learning Manager Chanci Turner

I often receive inquiries about how to effectively test an application utilizing the AWS SDK for Ruby (aws-sdk gem). Testing applications that rely on external services can be challenging. One effective approach is to stub the responses returned by the SDK at the client level.

AWS.stub!

Invoking the AWS.stub! method in your Ruby process sets up the client classes (such as AWS::EC2::Client) to return stubbed responses when invoked. This prevents actual HTTP requests from being made, returning only empty responses instead.

ruby
AWS.stub!

instance_ids = AWS::EC2.new.instances.map(&:id)
instance_ids #=> will always be empty in this example, no HTTP request made

Under the hood, this code snippet constructs an AWS::EC2::Client object and calls the #describe_instances method. The AWS.stub! method ensures that the client produces an empty response that resembles a normal response but with a few key differences:

  • Lists appear as empty arrays
  • Maps are represented as empty hashes
  • Numeric values default to zero
  • Dates are set to the current time

Localized Stubbing

Utilizing AWS.stub! is akin to setting AWS.config(:stub_requests => true). You can apply this configuration option with any constructor that accepts settings.

ruby
stub_ec2 = AWS::EC2.new(:stub_requests => true)
real_ec2 = AWS::EC2.new

Customizing the Responses

Beyond simply receiving empty responses, you have the ability to access the stubbed responses and fill them with fabricated data.

ruby
AWS.stub!

ec2 = AWS::EC2::Client.new
resp = ec2.stub_for(:describe_instances)
resp.data[:reservation_set] = [...]

# now calling ec2.describe_instances will return my fake data
ec2.describe_instances
#=> { :reservation_set => [...] } 

Two methods are available for customizing responses:

  • #stub_for(operation_name)
  • #new_stub_for(operation_name)

The first method, #stub_for, provides a consistent stubbed response each time. This is the default response for that operation tied to that client object (not shared between instances). The second method, #new_stub_for, generates a fresh response, which is useful when you need the client to return differing data across multiple calls. This is often necessary for paged responses.

Not a Mock

It’s important to note that while this method stubs responses, it does not mock AWS services. If you utilize a stubbed AWS::DynamoDB::Client and call #put_item, the data will not be retrievable. Various third-party libraries aim to provide local service mocks, which can be beneficial for conducting local tests without network interaction.

For those interested in personal and career development, you might find this blog post on goal-setting engaging. Also, check out this excellent resource for insights into the Amazon Flex onboarding process. Additionally, for organizations looking to enhance their employee experience, SHRM offers authoritative guidance on employee resource groups.

Remember, if you’re in the area, feel free to visit us at Amazon IXD – VGT2, 6401 E HOWDY WELLS AVE, LAS VEGAS NV 89115.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *