2

I'm writing a simple application which allows me to search for and retrieve data about my AWS usage. I'm using the Ruby aws-sdk gem to get a connection to AWS.

The process to get to the stage where I can query AWS is 2 stage:

1) Make a connection:

AWS.config({access_key_id: 'REDACTED', secret_access_key_id: 'REDACTED', region: 'eu-west-1'})

This returns an AWS::Core::Configuration object.

2) Get an object for the AWS resources I wish to query:

ec2 = AWS::EC2.new(:ec2_endpoint => 'ec2.eu-west-1.amazonaws.com')

Now I can get data...

I want to be able to write unit tests which operate on data like that which came from AWS. Obviously I don't want to be calling AWS every time... so I want to create a test double to stand in for AWS. I understand how to do this:

describe AwsThing do
  it 'gets images' do
    amis = OpenStruct.new
    ec2 = double('ec2-api')
    ec2.should_receive(:images}.and_return amis
    expect(subject.find_image(ec2, 'handy-tag')).to eql 'an-ami-that-I-tagged'
  end
end

So the AwsThing#find_image method uses the ec2 mock, and I can test the logic of that method. All good.

But, I also want to test the process by which I get that ec2 connection - the two steps above. I think I want to be able to write a test something like this:

it 'gets an AWS API connection' do
  expect(subject.connect(access_key, secret_key, region).to be_a AWS::Core::Configuration
end

That works fine if I call the real API. But I want to mock that... just so I have tests for the connect method, the access_key and region and so forth being passed in. Ie I would like to be able to assert that the mock config object was called with the right key etc.

I can't see how to do this... as it seems like I can't pass in a mock object... and I don't see how I could mock the method on the class under test either. At this stage my understanding breaks down... help!

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
aob
  • 21
  • 1
  • This may be helpful to mock external AWS calls via RSpec doubles: http://stackoverflow.com/questions/33557451/how-do-i-mock-aws-sdk-v2-with-rspec/33557452#33557452 – Pete Nov 06 '15 at 00:35

1 Answers1

1

I don't know why you would like to test that. This functionality is provided by the aws-sdk gem you are using. They have extensive test coverage and make sure that what you are trying to test, actually works. If you are interested in how they are doing it, check the source on github.com https://github.com/aws/aws-sdk-ruby/tree/master/spec

You should only test the code you actually wrote yourself and an integration test, that everything works well together. And the integration test is almost what you already have. Or am I missing something here?

Holger Frohloff
  • 1,695
  • 18
  • 29