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!