Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Support type-safe Do() and DoAndReturn() methods #634

Open
falsaffa opened this issue Apr 12, 2022 · 0 comments
Open

Support type-safe Do() and DoAndReturn() methods #634

falsaffa opened this issue Apr 12, 2022 · 0 comments

Comments

@falsaffa
Copy link

Requested feature

Similar to #622, having a type-safe Do() and DoAndReturn():

  1. Makes it easier to write Do() and DoAndReturn() code with autocomplete and type checks
  2. When functions are refactored, the type check of these functions helps identifies what tests need to be updated
  3. Protects against runtime issues that can be effectively caught during compile time or linting.

Given the interface:

type Foo interface {
    Bar(i int64, s string) (b bool)
}

gomock generates the following methods:

Do(f interface{})

DoAndReturn(f interface{})

Therefore, any function (even when it doesn't match the original interface's method signature) can be passed to Do() and DoAndReturn() risking a runtime error and making it difficult to refactor method signatures effectively.

Proposed Solution

Based on the example of, the following should become the signatures of Do() and DoAndReturn() of the generated mock:

Do(f func(int64, string))

DoAndReturn(f func(int64, string) bool)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant