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

Allow gomock to mock functions, not just interfaces #519

@cwmos

Description

@cwmos

Requested feature

Currently, if I run mockgen on a file, it only seems to generate mock versions of interfaces. I propose it should also generate mock versions of functions. So if my file contains a line like

type TestFunc func() int

Then mockgen should be able to generate a mocked instance of that function. So I should be able to write something like:

ctrl := gomock.NewController(t)
m := NewMockTestFunc(ctrl)
m.EXPECT().Return(101)
m.EXPECT().Return(102)
m.Func()() // Returns 101
m.Func()() // Returns 102

Why the feature is needed

Sometimes, dependencies to code are not interfaces but simple function callbacks. It would be nice if I can use gomock to mock these function callbacks. I understand a work around would be to wrap such a callback into an interface, but it would be nice if that was not needed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions