lostisland / faraday

Simple, but flexible HTTP client library, with support for multiple backends.
https://lostisland.github.io/faraday
MIT License
5.7k stars 971 forks source link

Would you be open to me contributing a `#clear` method in `Faraday::Adapter::Test::Stubs`? #1570

Open Decker87 opened 1 week ago

Decker87 commented 1 week ago

Hey folks! I've recently stumbled upon a challenge similar to what's documented in https://github.com/lostisland/faraday/issues/1041. That is, my application uses singleton classes with memoized methods to "cache" a Faraday instance. This works great in production but my tests will suffer from being unable to stub out different behaviors of the same URL/action in subsequent test examples.

I'm comfortable enough with the test adapter and stub code, and believe I could implement a method to clear the stubs reasonably well. Before I go off and spend time on that, could the maintainers of the project weigh in on whether such a thing would be in-line with the maintainers' goals and design philosophy?

Thanks in advance!

iMacTia commented 1 week ago

Hi @Decker87 and thank you so much for being considerate and asking before jumping into this.

When it comes to application code or API clients, my recommendation has always been to test using Webmock or VCR, as the test adapter was mostly conceived to help writing tests for Faraday middleware.

That said, there are multiple people who prefer using the test adapter for writing tests, and I completely agree the current suggestion of setting Faraday.default_connection = nil to reset stubs sounds more of a hack 🤔

I'd be open to introducing a clear method on Faraday::Adapter::Test::Stubs as you suggested and update the docs to suggest using that instead 👍