golang / mock

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

the func `gomock.Eq` not support func type param #555

Closed fmyxyz closed 3 years ago

fmyxyz commented 3 years ago

Actual behavior out:

        expected call at xxx_test.go:28 doesn't match the argument at index 0.
        Got: [0x1303340]
        Want: is equal to 0x1303340
    controller.go:266: missing call(s) to *mocks.MockStoreInterface.Set(is equal to 0x1303340) xxx_test.go:28
    controller.go:266: aborting test due to missing call(s)
--- FAIL: TestCacheInvalidate (0.00s)

Expected behavior no errors

To Reproduce

  1. code:
    var f= func() {fmt.Println("a func")}
    s.EXPECT().Set(f).Return(nil)
    s.Set(f)

Additional Information

Triage Notes for the Maintainers

suggest


func FuncEq(x interface{}) gomock.Matcher { return funEq{x} }

type funEq struct {
    x interface{}
}

func (f funEq) Matches(x interface{}) bool {
    xv := reflect.ValueOf(x)
    fxv := reflect.ValueOf(f.x)
    return xv.Pointer() == fxv.Pointer()
}

func (funEq) String() string {
    return "is same func"
}

or in eqMatcher judge type of x if it is func do:

xv := reflect.ValueOf(x)
    fxv := reflect.ValueOf(f.x)
    return xv.Pointer() == fxv.Pointer()
codyoss commented 3 years ago

I believe this is a duplicate of #324. I will close this issue in favor of that one. If this is something you are interested in I would gladly accept a PR :)