There is no documentation on testing with Faraday. Where I work Ruby on Rails was introduced because it's cool. Little thought was given to testing red-cases. I'd like to contribute some documentation to help others
usage
Thank you for bringing this up. In response, I have posted a PR to include live test/unit and rspec examples of using the Faraday Test adapter: #1000. This is the recommended way to test Faraday, instead of external test double libraries (rspec, mocha, webmock, etc).
I noticed at no point is it testing things such as timeouts, connection errors, etc. Is the subject frowned upon, or just the way PR #998 uses.
I suppose I don't get the syntax
I suppose I don't get the syntax
Oof, maybe I should revisit the Faraday test docs again :/
The syntax in #1000 uses a Faraday::Adapter::Test::Stubs
instance, an exclusive stub object to test faraday request/response cycles. You can use it to set up mock responses to http requests in a specific test.
it 'parses name' do
# this block yields a rack response: an array with:
# response status, http header hash, and response body
stubs.get('/ebi') do |env|
[
200,
{ 'Content-Type': 'application/javascript' },
'{"name": "shrimp"}'
]
end
expect(client.sushi('ebi')).to eq('shrimp')
stubs.verify_stubbed_calls
end
I apologize, I obviously didn't read your issues thoroughly enough. I totally missed that you specifically wanted to test exceptions. A test like this works too (which I will add to #1000):
it 'handles exception' do
stubs.get('/ebi') do
raise Faraday::ConnectionFailed, nil
end
expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
stubs.verify_stubbed_calls
end
I prefer specific test double implementations like this to the more generalized mocking approach, as they still go through the entire Faraday request workflow. If a developer wants to use RSpec mocks anyway, then they'll be better served with the excellent RSpec documentation.
Delivered with #1000
Most helpful comment
Thank you for bringing this up. In response, I have posted a PR to include live test/unit and rspec examples of using the Faraday Test adapter: #1000. This is the recommended way to test Faraday, instead of external test double libraries (rspec, mocha, webmock, etc).