Created
October 15, 2018 15:34
-
-
Save danhodge/6aca8382d72defa4680eeb934fd3aa3a to your computer and use it in GitHub Desktop.
Faraday Notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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