Skip to content

Instantly share code, notes, and snippets.

@bf4
Created November 4, 2013 22:19
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 bf4/7310171 to your computer and use it in GitHub Desktop.
Save bf4/7310171 to your computer and use it in GitHub Desktop.
require 'sinatra/base'
class FakeGitHub < Sinatra::Base
def simulate_error!
@@simulate_error = true
end
def reset!
@@simulate_error = false
end
def simulate_error?
@@simulate_error
end
before_do
if simulate_error?
halt 403, { errors: { first_name: "can't be blank" } }.to_json
end
end
get '/repos/:organization/:project/contributors' do
json_response 200, 'contributors.json'
end
get '/repos/:organization/:project/issues' do
json_response 200, 'issues.json'
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
feature 'External request' do
scenario 'GitHub service failure' do
FakeGithub.simulate_error!
uri = URI('https://api.github.com/repos/thoughtbot/factory_girl/contributors')
response = JSON.load(Net::HTTP.get(uri))
# some assertions
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment