matryer / moq

Interface mocking tool for go generate

Home Page:http://bit.ly/meetmoq

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Method to reset Call counters

MetalRex101 opened this issue · 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!

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?

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.

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, 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.

commented

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

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! ❤️