ruby - How to mock connection to external service -


i'm writing simple application allows me search , retrieve data aws usage. i'm using ruby aws-sdk gem connection aws.

the process stage can query aws 2 stage:

1) make connection:

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

this returns aws::core::configuration object.

2) object aws resources wish query:

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

now can data...

i want able write unit tests operate on data came aws. don't want calling aws every time... want create test double stand in aws. understand how this:

describe awsthing   'gets images'     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 awsthing#find_image method uses ec2 mock, , can test logic of method. good.

but, want test process ec2 connection - 2 steps above. think want able write test this:

it 'gets aws api connection'   expect(subject.connect(access_key, secret_key, region).to be_a aws::core::configuration end 

that works fine if call real api. want mock that... have tests connect method, access_key , region , forth being passed in. ie able assert mock config object called right key etc.

i can't see how this... seems can't pass in mock object... , don't see how mock method on class under test either. @ stage understanding breaks down... help!

i don't know why test that. functionality provided aws-sdk gem using. have extensive test coverage , make sure trying test, works. if interested in how doing it, check source on github.com https://github.com/aws/aws-sdk-ruby/tree/master/spec

you should test code wrote , integration test, works together. , integration test have. or missing here?


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -