golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.28k stars 610 forks source link

feat validate Do & DoReturn args #558

Closed codyoss closed 3 years ago

codyoss commented 3 years ago

Previous behavior was a panic would happen if the number of args did not match. Now the test should fail more gracefully and allow for better debugging.

Fixes: #71

bconway commented 3 years ago

Apologies, my reflection-fu is a little weak. Does this correctly handle variadic functions? I have a bunch of DoAndReturn with throw-away input in the form of .DoAndReturn(func(_ ...interface{}) error that worked without issue in 1.5.0 and now throw:

wrong number of arguments in DoAndReturn func for *notifier.Mockeventer.Create: got 1, want 2

in 1.6.0.

Or perhaps this is a Hyrum’s Law thing?

codyoss commented 3 years ago

@bconway Would you please open an issue with a simple example for discussion. This may be too strict in the case of varargs... need to take a closer look.

efekarakus commented 3 years ago

Hi! We also encountered the same behavior as @bconway on 1.6.0: https://github.com/golang/mock/issues/571 which broke our tests

schmurfy commented 3 years ago

I have the same issue, I reverted back to 1.5.0, variadic arguments are useful when you don't care about the argument themselves but want to do something when called.

codyoss commented 3 years ago

@schmurfy Please see my comments on #571

schmurfy commented 3 years ago

@codyoss I really not sure I agree with this being by design but I will survive, using Do(...interface{}) is a good way to clearly state in your test: I don't give a sh*t about the arguments and they are irrelevant in this context

When you see it that way having variadic arguments does have a meaning, it is not just there because the author was lazy.

In one of my tests using it calling a method will trigger two parallel call to an external interface, I have a waitgroup and use Do to watch the external calls simply calling wg.Done() in them.