mockk icon indicating copy to clipboard operation
mockk copied to clipboard

fix: prevent confirmVerified(vararg mocks) from clearing unrelated mocks

Open rsvinicius opened this issue 8 months ago • 3 comments

🚧 Draft Pull Request – Partial Fix for Issue #1379

This PR builds upon the changes introduced in #821, aiming to improve test isolation while addressing the regression reported in #1379.

🛠 What's Being Done

  • Refactored internalConfirmVerified to reset only the specified mocks when parameters are passed.
  • Updated resetVerificationState to selectively clear verification state for specific mocks.
  • Ensured cleanup with try-finally, making the verification process more robust.
  • Added a test case confirmVerifiedSpecificMock to verify that confirmVerified(mock) behavior is correct.

⚠️ Current Status

This PR is still in progress. While the targeted clearing works in isolated tests, the test suite fails when running confirmVerifiedAll.

This issue appears similar to the cross-test interference previously addressed in issue #821, but now occurs when confirmVerified is used without parameters after using it with specific mocks in other tests.

👉 Notably, if we add a clearAllMocks() call before the confirmVerifiedAll test, it passes successfully — just like it did before PR #1367.

🔍 Next Steps

Investigate and resolve the failure of confirmVerifiedAll in the full test suite

@Raibaz do you have any idea why this might be happening? It seems related to state retention when mixing confirmVerified(mock) and confirmVerified() in different tests.

rsvinicius avatar Apr 22 '25 17:04 rsvinicius

Wild guess: could it be that verify with more than one call, such as the one in the confirmVerifiedAll test, don't add the right values to the list of verified calls?

Can you check the values of allCalls and verifiedCalls here, when your test fails?

Raibaz avatar Apr 22 '25 18:04 Raibaz

Wild guess: could it be that verify with more than one call, such as the one in the confirmVerifiedAll test, don't add the right values to the list of verified calls?

I don’t think that’s the root cause, because when the confirmVerifiedAll test is run in isolation, it passes — so it seems that verify { ... } with multiple calls is working as expected in that context.

However, when the whole test suite is executed, the same test fails. That suggests some shared state leakage or interference between tests, similar to what we saw before the fix for #821.

Can you check the values of allCalls and verifiedCalls here, when your test fails?

Yes — I added debug prints and here’s what I found during a failing run (screenshot attached below). So it looks like the verifiedCalls list gets cleared unexpectedly across tests. Interestingly, if I add a clearAllMocks() at the beginning of the confirmVerifiedAll test, it passes — just like it used to before PR #1367.

image

rsvinicius avatar Apr 22 '25 19:04 rsvinicius

That's interesting - I'd do more investigation to understand who's clearing the verifiedCalls list, when and why :)

Raibaz avatar Apr 28 '25 20:04 Raibaz