No commit activity in last 3 years
No release in over 3 years
A Faraday extension to provide dynamic stubbing.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 0
 Project Readme

FaradaySimulation

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.

Usage

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

conn = Faraday::Connection.new { |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

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