Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing page for new documentation #997

Closed
Lewiscowles1986 opened this issue Jul 9, 2019 · 4 comments
Closed

Testing page for new documentation #997

Lewiscowles1986 opened this issue Jul 9, 2019 · 4 comments

Comments

@Lewiscowles1986
Copy link

Basic Info

  • Faraday Version: master
  • Ruby Version: 2.6.3

Issue description

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

Steps to reproduce

@Lewiscowles1986 Lewiscowles1986 mentioned this issue Jul 9, 2019
2 tasks
@technoweenie
Copy link
Member

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).

@Lewiscowles1986
Copy link
Author

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

@technoweenie
Copy link
Member

technoweenie commented Jul 18, 2019

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.

@iMacTia
Copy link
Member

iMacTia commented Feb 9, 2020

Delivered with #1000

@iMacTia iMacTia closed this as completed Feb 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants