Skip to content

Commit 9ab0d20

Browse files
committed
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
1 parent 9c8a2ee commit 9ab0d20

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

lib/active_resource/railtie.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,11 @@ class Railtie < Rails::Railtie
2727
app.deprecators[:active_resource] = ActiveResource.deprecator
2828
end
2929
end
30+
31+
initializer "active_resource.http_mock" do
32+
ActiveResource.on_load(:active_support_test_case) do
33+
teardown { ActiveResource::HttpMock.reset! }
34+
end
35+
end
3036
end
3137
end

0 commit comments

Comments
 (0)