Skip to content

Instantly share code, notes, and snippets.

@danhodge
Created October 15, 2018 15:34
Show Gist options
  • Save danhodge/6aca8382d72defa4680eeb934fd3aa3a to your computer and use it in GitHub Desktop.
Save danhodge/6aca8382d72defa4680eeb934fd3aa3a to your computer and use it in GitHub Desktop.
Faraday Notes
# How to stub out Faraday for testing
# The last middleware in the list is the innermost one and must be the HTTP adapter.
# Generally, it will be a single argument (a symbol), such as :net_http
#
# For example:
#
Faraday.new(url: 'http://example.com') do |conn|
# POST/PUT params encoders:
conn.request :multipart
conn.request :url_encoded
# Last middleware must be the adapter:
conn.adapter :net_http
end
# HTTP requests can be stubbed out by replacing the adapter with the test adapter, which requires two arguments,
# the adapter name symbol (:test) and the set of stubbed out interactions (a Faraday::Adapter::Test::Stubs object).
#
# For example:
#
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
stub.post(path) do |_env|
[200, {}, { success: true, team: { id: team_id } }.to_json]
end
end
Faraday.new(url: 'http://example.com') do |conn|
# POST/PUT params encoders:
conn.request :multipart
conn.request :url_encoded
# Last middleware must be the adapter:
conn.adapter :test, stubs
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment