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


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.