mock icon indicating copy to clipboard operation
mock copied to clipboard

Do() should panic when given a function that returns values

Open ash2k opened this issue 2 years ago • 3 comments

I've just spent an hour debugging a test where I by mistake used Do(func () string { ... return "some value" }) instead of DoAndReturn(). Do() just discarded the value and mock returned an empty string.

Do() should error out ASAP when given a function that returns values.

Thank you for the library!

ash2k avatar Sep 04 '23 05:09 ash2k

If I understand your issue correctly, this proposal turns a current no-op operation into a panic, which may break lots of existing tests that rely on this no-op behavior. One way to address this seems to be some type of linting to prevent this.

r-hang avatar Sep 05 '23 18:09 r-hang

Yes. There may also be tests that pass but don't actually test what the author thinks they are testing.

ash2k avatar Sep 05 '23 22:09 ash2k

Maybe it's worth excluding the return values from the function passed to Do? It's about the case when we pass the -typed flag to mockgen. Example:

type SomeInterface{
    Action(value int) (bool, error)
}

func (MockSomeInterface) Do(func(value int)) {...}

func (MockSomeInterface) DoAndReturn(func(value int) (bool, error)) {...}

tulzke avatar Sep 13 '23 13:09 tulzke