Skip to content

Conversation

seanpdoyle
Copy link
Contributor

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.

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
@rafaelfranca rafaelfranca merged commit f7a27f4 into rails:main Sep 10, 2025
19 checks passed
@seanpdoyle seanpdoyle deleted the http-mock-reset branch September 10, 2025 12:52
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 this pull request may close these issues.

2 participants