question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any way to clear all mocks after each data testing?

See original GitHub issue

Problem

I want to clear mocks after each data testing. The problem case is following.

forAll(
  row(..),
  row(..),
) { x, y ->
  sut.execute()
  verify { mockked.handle() }
}

In this case, verifying mockked.handle() may fail, because in this test function, single mockked instance is used.

Ugly Solution

Currently, the second best solution is just to append clearAllMocks() after each data testing.

forAll(
  row(..),
  row(..),
) { x, y ->
  sut.execute()
  verify { mockked.handle() }
  clearAllMocks() // Ugly code.
}

Is there any idea for this problem?

FYI. I used mockk.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
sksamuelcommented, Aug 16, 2022

data-driven-testing gets mapped to actual tests, so you can use the regular beforeTest {} callback. But not in annotation-spec.

1reaction
sksamuelcommented, Aug 28, 2022

Thanks, docs updated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to automatically reset mocks and restore spies in Jest
Configuring Jest to automatically reset / restore mocks and spies · “clearMocks”: true : resets all the mocks usage data, but keeps the...
Read more >
How to reset Jest mock functions calls count before every test
One way I found to handle it: to clear mock function after each test: To add to Sum.test.js: afterEach(() => { local.getData.
Read more >
Jest set, clear and reset mock/spy/stub implementation
Another way to do it is to clearAllMocks , this will mean that between tests, the stubs/mocks/spies are cleared, no matter which mock...
Read more >
jest.clearAllMocks(); does not remove mock implementation ...
in my test I'm trying to clear the mocks after each test ... remove the mock implementation, and you're saying there's no way...
Read more >
Mockito - Resetting Mock - Tutorialspoint
Here we've reset mock object. MathApplication makes use of calcService and after reset the mock, using mocked method will fail the test. Example....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found