uber-go / mock

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

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

Open ash2k opened 10 months ago

ash2k commented 10 months ago

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!

r-hang commented 10 months ago

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.

ash2k commented 10 months ago

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

tulzke commented 9 months ago

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)) {...}