moq icon indicating copy to clipboard operation
moq copied to clipboard

Method to reset Call counters

Open MetalRex101 opened this issue 3 years ago • 5 comments

It would be very helpful to have something live ResetCalls method to to reset all Call counters (if possible) and Per method reset Call counter methods. Sometimes you doesn't have direct access to mock and to avoid doing something like that:

mock := GetMock()
freshCountersMock := &Mock{}
*mock = *freshCountersMock

it would be very helpful to just call reset methods. Thank you!

MetalRex101 avatar Sep 15 '21 18:09 MetalRex101

A bit hesitant to increase the API surface area, not sure if it would be useful for the majority. Perhaps it could be added as a method depending upon an optional flag that would be disabled by default. What do you think @matryer?

sudo-suhas avatar Sep 26 '21 13:09 sudo-suhas

I use moq since more than 3 years in production and I never had the need for such reset methods. With the above mentioned workaround already at hand, I would prefer to not increase the API surface of moq.

breml avatar Oct 04 '21 09:10 breml

I think if there's a workaround in user space, we should opt for that.

@MetalRex101 can you share a little more about why you can't access the mocks? Usually you set them up in test code, and just assign them or pass them into a function. Are you doing something different?

matryer avatar Oct 04 '21 11:10 matryer

@matryer, i use IoC container. It is possible to reset mock in client code, but it looks very clunky, because you have to cast you dependency to mock type first and then substitute it the way i described above. Since it is a good practice in go to use test suites with the table tests approach, if you need to set up everything once and then just clear counters values, it looks very nice to have special methods for that.

MetalRex101 avatar Oct 04 '21 11:10 MetalRex101

+1 for simple function to empty calls for a given mocked function

cbaker avatar Aug 04 '22 19:08 cbaker

On my case, I've been using moq for quite a long time without needed this feature until today #181 is exactly what I'd need.

For some e2e tests, I create a mocked service at the beginning of the tests and then assert that it has received the specified calls. In my case it is more expensive to initialize this mocked service before each tests instead of at the beginning of the test suite, if I could reset the mock calles before each test that would help me start each test in a clean environment.

By the way, thanks for this project, it is very helpful and I love it! ❤️

efueyo avatar Nov 12 '22 04:11 efueyo

Closed via #181 and released in v0.3.1.

sudo-suhas avatar Mar 08 '23 05:03 sudo-suhas