-
-
Notifications
You must be signed in to change notification settings - Fork 331
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
Doc change: clarify difference between clear and unmock #184
Comments
True, even worse. I am not able to finalize understanding of what cases should be handled by what functions. Which patterns are right to effectively clean-up it. And in the code there is a mess caused by compatibility with previous implementations (as there was an attempt to change things to better). Simply of course, I think the problem is that I am myself not using my library a lot and that's why hard to understand all this cleanup stuff. It is a bit coming to me as a nightmare from time to time |
Would you consider the following a good practise? If that's correct and if you agree with such usage, I can make a PR suggesting changes to some documentation and MockK's readme. I wonder what would be a use reason to unmock an object. Maybe to unmock static mocks so that between tests you can use or mock them differently? |
My current practice is the following:
Would you consider this good practice (in general)? If so, please consider suggesting this in the documentation. Currently only |
I observe the opposite behaviour (v. 1.9.3) where only |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as |
Added to docs: https://mockk.io/#clearing-vs-unmocking |
This is a still a bit unclear for me. In the following case:
If I call |
My hunch is that the objects will be cleared if you call |
Clearing resets the mock's state: it resets method/property access counts, mocked behaviour (e.g. set through After clearing a mock, you can still (re)use the mock. For example: val mock = mockk<Any>()
every { mock.toString() } returns "foo"
assertEquals("foo", mock.toString())
verify(exactly = 1) { mock.toString() }
clearMocks(mock)
verify(exactly = 0) { mock.toString() } // Mock is reused for verification, but `toString` call count was reset I usually instantiate mocks once per test class, i.e. in the companion object (for performance reasons), clear all mocks after every test function, and unmock all mocks after every test class. I have published a library that does the clearing and unmocking for you: https://github.com/erikhuizinga/mockk-patterns, so you don't have to remember that. |
Expected Behavior
There is documentation explaining the specific use cases for the various
unmock
functions w.r.t. theclear
functions, and the differences.Current Behavior
It is unclear what 'clear' vs. 'cancel' vs. 'unmock' mean. Take a look at the two following pieces of documentation.
The documentation of, for example,
unmockAll
:What does 'unmock' mean? What does 'cancel' mean? Are they the same? When do I unmock?
The documentation of, for example,
clearAllMocks
:What does 'clear' mean w.r.t. 'unmock' or 'cancel'?
Context
The text was updated successfully, but these errors were encountered: