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

Reset ActiveResource::HttpMock in test teardown #412

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

seanpdoyle
Copy link

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

1 participant