Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
51 lines (33 sloc) 1.48 KB

Artifice allows you to replace the Net::HTTP subsystem of Ruby
with an equivalent that routes all requests to a Rack application.

You can use Sinatra, raw Rack, or even Rails as your application,
allowing you to build up an equivalent to the remote service you
are mocking out using familiar and convenient tools to route
requests and build up responses.


First, require artifice.

require "artifice"

Next, activate artifice using a Rack endpoint.


You’re done!

You can also pass a block to activate_with:

Artifice.activate_with(rack_endpoint) do
  # make some requests using Net::HTTP

This will replace Net::HTTP for the duration of the block only.

You can deactivate Artifice by calling deactivate


Artifice will restore the last endpoint after a block-style call, so the following will work as expected:


Artifice.activate_with(block_endpoint) do
  # make some requests using Net::HTTP

# make any requests after the block and they'll hit the global_endpoint

Furthermore, Artifice will allow you to nest block-style calls to activate_with like so:

Artifice.activate_with(outer_endpoint) do
  # make some requests that hit an endpoint
  Artifice.activate_with(inner_endpoint) do
    # make some requests that require a different endpoint
  # make some other requests that will hit the outer block's endpoint