« Back to Index

Ruby WebMock Example

View original Gist on GitHub

Ruby WebMock Example.md

When using WebMock with RSpec:

require 'webmock'
require 'webmock/rspec'

before do
  WebMock.stub_request(:get, foo_endpoint).to_timeout
  WebMock.stub_request(:get, bar_endpoint).to_return(:body => "bar stuff")
end

When using WebMock outside of a testing framework don’t forget WebMock.enable!:

require "faraday"
require "webmock"
require "pry"

WebMock.enable!

endpoint = "http://www.beepbeepbeepboop.com"

# Notice we'll stub ANY type of request (GET, POST, PUT etc)
WebMock.stub_request(:any, endpoint).to_return(:body => "something else")

p Faraday.get(endpoint).body