Skip to content

Instantly share code, notes, and snippets.

@harlow
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harlow/58a10743b32e6b3dd2af to your computer and use it in GitHub Desktop.
Save harlow/58a10743b32e6b3dd2af to your computer and use it in GitHub Desktop.
Have stubbed service emulate failures
require 'sinatra/base'
class FakeGitHub < Sinatra::Base
cattr_reader :emulate_failure
def self.reset!
@@emulate_failure = false
end
def self.emulate_failure!
@@emulate_failure = true
end
get '/repos/:organization/:project/contributors' do
if emulate_failure
json_response 400, 'error.json'
else
json_response 200, 'contributors.json'
end
end
private
def json_response(response_code, file_name)
content_type :json
status response_code
File.open(File.dirname(__FILE__) + '/fixtures/' + file_name, 'rb').read
end
end
require 'spec_helper'
feature 'External request' do
after do
FakeGitHub.reset!
end
it 'queries FactoryGirl contributors on Github' do
FakeGitHub.emulate_failure!
uri = URI('https://api.github.com/repos/thoughtbot/factory_girl/contributors')
expect(response).to have_http_status(:bad_request)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment