{"id":"f54d7c1c626d395c","repo":"stretchr/testify","slug":"cannot-use-func-in-expectations-use-mock-anything","errorCode":null,"errorMessage":"cannot use Func in expectations. Use mock.AnythingOfType(\"%T\")","messagePattern":"cannot use Func in expectations\\. Use mock\\.AnythingOfType\\(\"%T\"\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mock/mock.go","lineNumber":226,"sourceCode":"\treturn c.Parent.On(methodName, arguments...)\n}\n\n// Unset removes all mock handlers that satisfy the call instance arguments from being\n// called. Only supported on call instances with static input arguments.\n//\n// For example, the only handler remaining after the following would be \"MyMethod(2, 2)\":\n//\n//\tMock.\n//\t   On(\"MyMethod\", 2, 2).Return(0).\n//\t   On(\"MyMethod\", 3, 3).Return(0).\n//\t   On(\"MyMethod\", Anything, Anything).Return(0)\n//\tMock.On(\"MyMethod\", 3, 3).Unset()\nfunc (c *Call) Unset() *Call {\n\tvar unlockOnce sync.Once\n\n\tfor _, arg := range c.Arguments {\n\t\tif v := reflect.ValueOf(arg); v.Kind() == reflect.Func {\n\t\t\tpanic(fmt.Sprintf(\"cannot use Func in expectations. Use mock.AnythingOfType(\\\"%T\\\")\", arg))\n\t\t}\n\t}\n\n\tc.lock()\n\tdefer unlockOnce.Do(c.unlock)\n\n\tfoundMatchingCall := false\n\n\t// in-place filter slice for calls to be removed - iterate from 0'th to last skipping unnecessary ones\n\tvar index int // write index\n\tfor _, call := range c.Parent.ExpectedCalls {\n\t\tif call.Method == c.Method {\n\t\t\t_, diffCount := call.Arguments.Diff(c.Arguments)\n\t\t\tif diffCount == 0 {\n\t\t\t\tfoundMatchingCall = true\n\t\t\t\t// Remove from ExpectedCalls - just skip it\n\t\t\t\tcontinue\n\t\t\t}","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/stretchr/testify/blob/001eb7946baf451879253643e4ce4b38eaa0d4a7/mock/mock.go#L208-L244","documentation":"Panicked by Call.Unset() (mock/mock.go:221) when any of the Call's recorded Arguments is a reflect.Func. Unset removes handlers by matching static argument values via Arguments.Diff; func values cannot be reliably matched this way, so testify refuses. The guard iterates c.Arguments and checks reflect.ValueOf(arg).Kind() == reflect.Func (mock/mock.go:224-227).","triggerScenarios":"Calling Mock.On(\"Method\", someFunc).Return(...).Unset() — i.e. trying to remove an expectation whose arguments include a function value. Also when an argument was registered with mock.MatchedBy (which wraps a func) and you later Unset.","commonSituations":"Test teardown that tries to remove a previously-set expectation which used a func matcher; refactoring tests to use is-functional-argument matchers without updating the Unset call.","solutions":["Use static (non-func) argument values in the Mock.On call so Unset can match them deterministically.","Use mock.AnythingOfType(\"func()\") instead of passing the func directly, then Unset works against the type matcher.","If you only need to clear all expectations, call Mock.Mock.ExpectedCalls = nil or re-create the Mock instead of Unset."],"exampleFix":"// before\nm.On(\"Run\", func() {}).Return(nil).Unset() // panic\n// after\nm.On(\"Run\", mock.AnythingOfType(\"func()\")).Return(nil)","handlingStrategy":"type-guard","validationCode":"func hasFuncArg(args []interface{}) bool {\n    for _, a := range args {\n        if reflect.ValueOf(a).Kind() == reflect.Func { return true }\n    }\n    return false\n}\n// before .Unset(): assert.False(t, hasFuncArg(call.Arguments))","typeGuard":"func argsContainFunc(args []interface{}) bool {\n    for _, a := range args {\n        if reflect.ValueOf(a).Kind() == reflect.Func { return true }\n    }\n    return false\n}","tryCatchPattern":"// Skip Unset when a func argument is present:\nif !argsContainFunc(call.Arguments) {\n    call.Unset()\n}","preventionTips":["Register expectations with static values or AnythingOfType, never raw funcs.","Keep a helper that builds Mock.On calls to enforce non-func arguments.","Review Unset calls whenever a matcher is introduced."],"tags":["mock","panic","unset","func-argument"],"analyzedSha":"001eb7946baf451879253643e4ce4b38eaa0d4a7","analyzedAt":"2026-08-04T21:49:58.715Z","schemaVersion":2}