{"id":"1c5cc0bc48eed9a9","repo":"stretchr/testify","slug":"cannot-take-func-type-as-argument","errorCode":null,"errorMessage":"cannot take func type as argument","messagePattern":"cannot take func type as argument","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"assert/assertions.go","lineNumber":524,"sourceCode":"\t\tdiff := diff(expected, actual)\n\t\texpected, actual = formatUnequalValues(expected, actual)\n\t\treturn Fail(t, fmt.Sprintf(\"Not equal: \\n\"+\n\t\t\t\"expected: %s\\n\"+\n\t\t\t\"actual  : %s%s\", expected, actual, diff), msgAndArgs...)\n\t}\n\n\treturn true\n}\n\n// validateEqualArgs checks whether provided arguments can be safely used in the\n// Equal/NotEqual functions.\nfunc validateEqualArgs(expected, actual interface{}) error {\n\tif expected == nil && actual == nil {\n\t\treturn nil\n\t}\n\n\tif isFunction(expected) || isFunction(actual) {\n\t\treturn errors.New(\"cannot take func type as argument\")\n\t}\n\treturn nil\n}\n\n// Same asserts that two pointers reference the same object.\n//\n//\tassert.Same(t, ptr1, ptr2)\n//\n// Both arguments must be pointer variables. Pointer variable sameness is\n// determined based on the equality of both type and value.\nfunc Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {\n\tif h, ok := t.(tHelper); ok {\n\t\th.Helper()\n\t}\n\n\tsame, ok := samePointers(expected, actual)\n\tif !ok {\n\t\treturn Fail(t, \"Both arguments must be pointers\", msgAndArgs...)","sourceCodeStart":506,"sourceCodeEnd":542,"githubUrl":"https://github.com/stretchr/testify/blob/001eb7946baf451879253643e4ce4b38eaa0d4a7/assert/assertions.go#L506-L542","documentation":"Returned by validateEqualArgs (assert/assertions.go:518) when assert.Equal or assert.NotEqual is called with an argument whose reflect.Kind is reflect.Func. Functions are not comparable by reflect.DeepEqual in a meaningful identity sense, so testify rejects them outright to avoid producing misleading pass/fail results. The check is made via isFunction() (assert/assertions.go:1975) which inspects reflect.TypeOf(arg).Kind().","triggerScenarios":"Calling assert.Equal(t, someFunc, otherFunc) or assert.NotEqual(t, fn, nil) where one side is a func value. Also triggered indirectly by assert.True/ObjectsAreEqualValues chains that route through Equal when a struct field happens to be a function type.","commonSituations":"Comparing handler/callback fields inside structs, asserting on http.HandlerFunc values, or passing a closure as an expected value by mistake instead of its return value. Common after refactors that turn a field from a value into a function.","solutions":["Compare a non-comparable proxy instead of the func: compare the func's reflect.Pointer address, or assert on a captured return value.","If you only care that both sides are non-nil funcs, use assert.NotNil on each side separately rather than Equal.","If comparing func types (not values), use assert.IsType(t, (*func())(nil), actual) or mock.AnythingOfType(\"func()\")."],"exampleFix":"// before\nassert.Equal(t, handler.ServeHTTP, got)\n// after\nassert.NotNil(t, got)","handlingStrategy":"type-guard","validationCode":"func isFuncArg(v interface{}) bool {\n    if v == nil { return false }\n    return reflect.TypeOf(v).Kind() == reflect.Func\n}\n// before assert.Equal: assert.False(t, isFuncArg(expected) || isFuncArg(actual))","typeGuard":"func isFuncValue(v interface{}) bool {\n    if v == nil { return false }\n    return reflect.TypeOf(v).Kind() == reflect.Func\n}","tryCatchPattern":"// Go has no try/catch; validate before calling:\nif isFuncValue(expected) || isFuncValue(actual) {\n    t.Skip(\"cannot Equal func values\")\n}\nassert.Equal(t, expected, actual)","preventionTips":["Never pass func values to assert.Equal/NotEqual; compare a derived value instead.","Lint for assert.Equal calls where either operand is known to be a function type.","When comparing structs, ensure no exported field is a func type."],"tags":["assertion","reflection","func-type","equal"],"analyzedSha":"001eb7946baf451879253643e4ce4b38eaa0d4a7","analyzedAt":"2026-08-04T21:49:58.715Z","schemaVersion":2}