Today I Learned

Stubbing responses from AWS services

We have started integration with Amazon SQS recently and did need to write some unit tests related to it. Unfortunately stubbing AWS client library the regular way turned out to be pretty cumbersome and challenging. Fortunately AWS SDK for ruby provides tools that make it pretty comfortable.

# Simple stubbing...
sqs_response_mock = Aws::SQS::Types::ReceiveMessageResult.new
sqs_response_mock.messages << Aws::SQS::Types::Message.new(body: 'abc')
Aws.config[:sqs] = {
    stub_responses: {
        receive_message: sqs_response_mock
    }
}

# ...allows properly polling the queue
poller = Aws::SQS::QueuePoller.new('https://sqs.us-east-2.amazonaws.com/123/some-queue')
poller.poll do |msg|
  puts msg.body
end

# => abc

Documentation can be found here