Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A Faraday extension to provide dynamic stubbing.

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 MIT-LICENSE
Octocat-spinner-32 Rakefile
Octocat-spinner-32 faraday_simulation.gemspec


A Faraday extension to provide dynamic stubbing. This makes it much easier to simulate the behavior of the target service in your tests and when your app is running in development mode.


The FaradaySimulation::Adapter class is the equivalent of Faraday's own test adapter, but the stubbing is more powerful:

conn = { |builder|
  builder.use(FaradaySimulation::Adapter) { |stub|
    stub.get('/drink/:name.json') { |env|
      [200, {}, "Drinking #{env[:params]['name']}"]

resp = conn.get('/drink/sake.json')
# resp.body => 'Drinking sake'

resp = conn.get('/drink/ale.json')
# resp.body => 'Drinking ale'

Put simply, any stub path you define that includes :foo will match to any string, and that string will be the value of env[:params]['foo'] in your stub block.

For more complex routes, you can define the stub with a regular expression. Any regex group in the matching regex will be placed in env[:segments]. For example:

stub.get(/\/foo\/([A-Z]+)\//) { |env|
  [200, {}, "Segment is #{env[:segments][0]}"]

resp = conn.get('/foo/BAR')
# resp.body => 'Segment is BAR'

resp = conn.get('/foo/bar')
# => Stubs::NotFound error


Copyright (c) 2012 Joseph Pearson, released under the MIT License. See MIT-LICENSE for details.

Something went wrong with that request. Please try again.