Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Webmock with RSpec
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
You can’t perform that action at this time.