mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-03 00:02:48 -04:00
Previously, the Faraday testing adapter compared the request body to the stubbed body just by calling `#==` if the stubbed body is present. Sometimes, I want to check the equality between request and stubbed body in more advanced ways. For example, there is a case that I want to check only the parts of the body are actually passed to Faraday instance like this: ```ruby stubs = Faraday::Adapter::Test::Stubs.new do |stub| stub.post('/foo', '{"name:"YK","created_at":"ANY STRING IS OK"}') { [200, {}, ''] } end connection.post('/foo', JSON.dump(name: 'YK', created_at: Time.now)) stubs.verify_stubbed_calls ``` In this case, it's difficult to make tests always pass with `"created_at"` because the value is dynamic. So, I came up with an idea to pass a proc as a stubbed value and compare bodies, inside the proc: ```ruby stubs = Faraday::Adapter::Test::Stubs.new do |stub| check = -> (request_body) { JSON.parse(request_body).slice('name') == { 'name' => 'YK' } } stub.post('/foo', check) { [200, {}, ''] } end connection.post('/foo', JSON.dump(name: 'YK', created_at: Time.now)) stubs.verify_stubbed_calls ``` I believe this would be flexible but compatible with the previous behavior.