From 388454c8291fc24e75de275c151264e256b6f6f9 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Sat, 4 Jan 2025 11:21:33 -0500 Subject: [PATCH] Reset `ActiveResource::HttpMock` in test teardown Calls to `ActiveResource::HttpMock.respond_to` without arguments reset the mock, clearing previously defined request-response pairs. Invocations with a final positional argument of `false` **do not clear** previously defined request-response mocks. This behavior can be useful for tests that require a sequence of Http mock definitions. Regardless of the style of invocation, the collection of mocks should be reset between individual tests, so that mock state does not leak into subsequent test cases, which has the potential to cause unpredictable behavior and flaky tests. This change defines an `ActiveSupport.on_load` hook to execute for `ActiveSupport::TestCase` instances. The hook defines a [teardown][] block to invoke `ActiveResource::HttpMock#reset!` between test cases. [teardown]: https://edgeapi.rubyonrails.org/classes/ActiveSupport/Testing/SetupAndTeardown/ClassMethods.html#method-i-teardown --- lib/active_resource/railtie.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/active_resource/railtie.rb b/lib/active_resource/railtie.rb index dc4e08d6af..3ca9be804b 100644 --- a/lib/active_resource/railtie.rb +++ b/lib/active_resource/railtie.rb @@ -27,5 +27,11 @@ class Railtie < Rails::Railtie app.deprecators[:active_resource] = ActiveResource.deprecator end end + + initializer "active_resource.http_mock" do + ActiveSupport.on_load(:active_support_test_case) do + teardown { ActiveResource::HttpMock.reset! } + end + end end end