matryer / moq

Interface mocking tool for go generate
http://bit.ly/meetmoq
MIT License
2k stars 127 forks source link

Method to reset Call counters #157

Closed MetalRex101 closed 1 year ago

MetalRex101 commented 3 years ago

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!

sudo-suhas commented 3 years ago

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?

breml commented 3 years ago

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.

matryer commented 3 years ago

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?

MetalRex101 commented 3 years ago

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

cbaker commented 2 years ago

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

efueyo commented 2 years ago

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

sudo-suhas commented 1 year ago

Closed via #181 and released in v0.3.1.