{"id":"57457e2ab24a14c9","repo":"stretchr/testify","slug":"assert-arguments-s-does-not-take-exactly-one-ar","errorCode":null,"errorMessage":"assert: arguments: %s does not take exactly one argument","messagePattern":"assert: arguments: (.+?) does not take exactly one argument","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mock/mock.go","lineNumber":928,"sourceCode":"// 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}\n\n// Is gets whether the objects match the arguments specified.\nfunc (args Arguments) Is(objects ...interface{}) bool {","sourceCodeStart":910,"sourceCodeEnd":946,"githubUrl":"https://github.com/stretchr/testify/blob/001eb7946baf451879253643e4ce4b38eaa0d4a7/mock/mock.go#L910-L946","documentation":"Panicked by MatchedBy() (mock/mock.go:921) when the passed function's reflect.Type reports NumIn() != 1. MatchedBy calls fn with exactly one argument (the mocked call argument at that position), so a zero- or multi-arg function is invalid. Check at mock/mock.go:927.","triggerScenarios":"Calling mock.MatchedBy(func(a, b int) bool { ... }) or mock.MatchedBy(func() bool { ... }) — a predicate with the wrong arity.","commonSituations":"Reusing an existing two-argument predicate function as a matcher; refactoring a matcher's signature and forgetting it must take exactly one parameter.","solutions":["Ensure the matcher function takes exactly one argument: mock.MatchedBy(func(singleArg T) bool { ... }).","If you need multiple values, match on a struct/struct-pointer argument that bundles them.","Wrap an existing multi-arg predicate in a single-arg closure that unpacks the fields."],"exampleFix":"// before\nm.On(\"Do\", mock.MatchedBy(func(a, b int) bool { return a+b > 0 })).Return(nil)\n// after\nm.On(\"Do\", mock.MatchedBy(func(p Pair) bool { return p.A+p.B > 0 })).Return(nil)","handlingStrategy":"validation","validationCode":"func takesOneArg(fn interface{}) bool {\n    t := reflect.TypeOf(fn)\n    return t != nil && t.Kind() == reflect.Func && t.NumIn() == 1\n}\n// before MatchedBy: assert.True(t, takesOneArg(matcher))","typeGuard":"func matcherTakesOneArg(fn interface{}) bool {\n    t := reflect.TypeOf(fn)\n    return t != nil && t.Kind() == reflect.Func && t.NumIn() == 1\n}","tryCatchPattern":"// Validate arity before constructing the matcher:\nif !matcherTakesOneArg(matcher) {\n    log.Fatal(\"matcher must take exactly one argument\")\n}\nm.On(\"Do\", mock.MatchedBy(matcher))","preventionTips":["Standardize matchers as func(T) bool in a single package.","Review MatchedBy predicates when refactoring argument types."],"tags":["mock","panic","matchedby","arity"],"analyzedSha":"001eb7946baf451879253643e4ce4b38eaa0d4a7","analyzedAt":"2026-08-04T21:49:58.715Z","schemaVersion":2}