Skip to content

Instantly share code, notes, and snippets.

@hedgehog
Forked from mrchrisadams/api_wrapper_spec.rb
Created November 29, 2011 20:30
Show Gist options
  • Save hedgehog/1406343 to your computer and use it in GitHub Desktop.
Save hedgehog/1406343 to your computer and use it in GitHub Desktop.
Using webmock and VCR together
describe ApiWrapper::Connection, "with retry enabled" do
before(:each) do
# connect to api
# the @api_wrapper object persists, across all the responses, and we set the number of retries
# on the ApiWrapper::Connection#get method here too, for when we call it further down in the spec.
@api_wrapper = ApiWrapper::Connection.new('stage.api_wrapper.com', APIWRAPPER_V2_API_KEY, APIWRAPPER_V2_PASSWORD, :retries => 2)
VCR.use_cassette("ApiWrapper_Connection/v2/raising unhandled errors") do
@api_wrapper.authenticate
end
end
it "should retry after ApiWrapper::TimeOut the correct number of times" do
# stub request twice then make it pass properly, assumign the data I'm after is in
# the second request I'd make, I'd call something like
#
response_body = Yaml.load_file('path/to/cassette.yml')[1]['request']['body']
stub_request(:any, "http://stage.api_wrapper.com/data").to_return(:status => 408, :body => "").times(2).
then.to_return(:status => 200, :body => response_body)
# I'm just testing the request is successful, I'd otherwise treat this like any other request
lambda {
@api_wrapper.get('/data')
}.should_not raise_error
end
it "should retry #{e.name} the correct number of times and raise error on failure" do
stub_request(:any, "http://stage.company.com/data").to_return(:status => 408, :body => "").times(3)
lambda {
@api_wrapper.get('/data')
}.should raise_error(e)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment