{"id":"0a33c27bd313ad89","repo":"stretchr/testify","slug":"assert-arguments-s-is-not-a-func","errorCode":null,"errorMessage":"assert: arguments: %s is not a func","messagePattern":"assert: arguments: (.+?) is not a func","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mock/mock.go","lineNumber":925,"sourceCode":"}\n\n// MatchedBy can be used to match a mock call based on only certain properties\n// from a complex struct or some calculation. It takes a function that will be\n// evaluated with the called argument and will return true when there's a match\n// and false otherwise.\n//\n// Example:\n//\n//\tm.On(\"Do\", MatchedBy(func(req *http.Request) bool { return req.Host == \"example.com\" }))\n//\n// fn must be a function accepting a single argument (of the expected type)\n// which returns a bool. If fn doesn't match the required signature,\n// MatchedBy() panics.\nfunc MatchedBy(fn interface{}) argumentMatcher {\n\tfnType := reflect.TypeOf(fn)\n\n\tif fnType.Kind() != reflect.Func {\n\t\tpanic(fmt.Sprintf(\"assert: arguments: %s is not a func\", fn))\n\t}\n\tif fnType.NumIn() != 1 {\n\t\tpanic(fmt.Sprintf(\"assert: arguments: %s does not take exactly one argument\", fn))\n\t}\n\tif fnType.NumOut() != 1 || fnType.Out(0).Kind() != reflect.Bool {\n\t\tpanic(fmt.Sprintf(\"assert: arguments: %s does not return a bool\", fn))\n\t}\n\n\treturn argumentMatcher{fn: reflect.ValueOf(fn)}\n}\n\n// Get Returns the argument at the specified index.\nfunc (args Arguments) Get(index int) interface{} {\n\tif index+1 > len(args) {\n\t\tpanic(fmt.Sprintf(\"assert: arguments: Cannot call Get(%d) because there are %d argument(s).\", index, len(args)))\n\t}\n\treturn args[index]\n}","sourceCodeStart":907,"sourceCodeEnd":943,"githubUrl":"https://github.com/stretchr/testify/blob/001eb7946baf451879253643e4ce4b38eaa0d4a7/mock/mock.go#L907-L943","documentation":"Panicked by MatchedBy() (mock/mock.go:921) when reflect.TypeOf(fn).Kind() != reflect.Func. MatchedBy builds an argumentMatcher that invokes fn against each call argument, so a non-function input is a programming error. The check is at mock/mock.go:924.","triggerScenarios":"Calling mock.MatchedBy(someStruct{}) or mock.MatchedBy(42) — passing anything other than a function. Typically a typo where the matcher predicate was forgotten.","commonSituations":"Refactoring that replaces a func with a struct method reference incorrectly; copy-paste errors; passing a method value that resolved to nil interface.","solutions":["Pass a function literal: mock.MatchedBy(func(x ExpectedType) bool { ... }).","Verify the variable passed to MatchedBy is actually a func with reflect.TypeOf(v).Kind() == reflect.Func.","If you meant to match a value, use the literal value directly in Mock.On instead of MatchedBy."],"exampleFix":"// before\nm.On(\"Set\", mock.MatchedBy(value)).Return(nil)\n// after\nm.On(\"Set\", mock.MatchedBy(func(v int) bool { return v > 0 })).Return(nil)","handlingStrategy":"type-guard","validationCode":"func isFunc(v interface{}) bool {\n    if v == nil { return false }\n    return reflect.TypeOf(v).Kind() == reflect.Func\n}\n// before MatchedBy: assert.True(t, isFunc(matcher))","typeGuard":"func isFunc(v interface{}) bool {\n    if v == nil { return false }\n    return reflect.TypeOf(v).Kind() == reflect.Func\n}","tryCatchPattern":"// Only call MatchedBy when the input is a func:\nif isFunc(matcher) {\n    m.On(\"Do\", mock.MatchedBy(matcher))\n}","preventionTips":["Always pass a function literal to MatchedBy, not a variable of unknown kind.","Add a unit test for your matcher-builder that asserts the result is a func."],"tags":["mock","panic","matchedby","func-argument"],"analyzedSha":"001eb7946baf451879253643e4ce4b38eaa0d4a7","analyzedAt":"2026-08-04T21:49:58.715Z","schemaVersion":2}