Created
September 29, 2011 18:56
-
-
Save CodeMonkeySteve/1251592 to your computer and use it in GitHub Desktop.
Webmock with RSpec
This file contains hidden or 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 'pathname' | |
require 'webmock/rspec' | |
module WebMock | |
# pass-through these domains (e.g. "www.facebook.com") | |
Allow = %w().freeze | |
end | |
RSpec.configure do |config| | |
stubs = {} | |
config.before(:suite) do | |
dir = config.fixture_path + '/webmock/' | |
Dir["#{dir}/**/*"].each do |path| | |
next if File.directory? path | |
uri = path.dup | |
uri.slice!( "#{dir}/" ) | |
uri = File.dirname(uri)+'/' if File.basename(uri) == '_directory' | |
stubs["http://#{uri}"] = path | |
#puts "http://#{uri}, #{path}" | |
end | |
end | |
config.around(:each) do |example| | |
stubs.each { |uri, path| WebMock::API.stub_request(:get, uri).to_return( File.new(path) ) } | |
WebMock.disable_net_connect!(:allow_localhost => true, :allow => WebMock::Allow) | |
example.call | |
WebMock.allow_net_connect! | |
end | |
end | |
class WebMock::NetConnectNotAllowedError | |
def stubbing_instructions(sig) | |
text = "You can stub this request with the following command:\n\n" | |
if sig.method == :get | |
dir = RSpec.configuration.fixture_path + '/webmock/' + sig.uri.host | |
dir += ":#{sig.uri.port}" if sig.uri.normalized_port | |
text << " mkdir -p '#{dir}' && curl -i -o '#{dir}#{sig.uri.omit(:scheme,:userinfo,:host,:port)}' '#{sig.uri}'" | |
else | |
text << WebMock::StubRequestSnippet.new(sig).to_s | |
end | |
text | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment