Kotest itself has no mock features. However, you can plug-in your favourite mocking library with ease!
Let's take for example mockk:
This example works as expected, but what if we add more tests that use that mockk?
The above snippet will cause an exception!
2 matching calls found, but needs at least 1 and at most 1 calls
This will happen because the mocks are not restarted between invocations. By default, Kotest isolates tests by creating a single instance of the spec for all the tests to run.
This leads to mocks being reused. But how can we fix this?
As for any function that is executed inside the Spec definition, you can place listeners at the end
Depending on the usage, playing with the IsolationMode for a given Spec might be a good option as well. Head over to isolation mode documentation if you want to understand it better.