Last active
August 29, 2015 14:16
-
-
Save harlow/58a10743b32e6b3dd2af to your computer and use it in GitHub Desktop.
Have stubbed service emulate failures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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