{"id":"c3159b58e495edee","repo":"stretchr/testify","slug":"assert-arguments-cannot-call-get-d-because-the","errorCode":null,"errorMessage":"assert: arguments: Cannot call Get(%d) because there are %d argument(s).","messagePattern":"assert: arguments: Cannot call Get\\((.+?)\\) because there are (.+?) argument\\(s\\)\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mock/mock.go","lineNumber":940,"sourceCode":"\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 {\n\tfor i, obj := range args {\n\t\tif obj != objects[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// Diff gets a string describing the differences between the arguments\n// and the specified objects.\n//\n// Returns the diff string and number of differences found.","sourceCodeStart":922,"sourceCodeEnd":958,"githubUrl":"https://github.com/stretchr/testify/blob/001eb7946baf451879253643e4ce4b38eaa0d4a7/mock/mock.go#L922-L958","documentation":"Panicked by Arguments.Get() (mock/mock.go:938) when index+1 > len(args). Get is the backing accessor for the typed extractors (String, Int, Error, Bool, etc.) and for direct argument retrieval in Run/Return callbacks. The panic message includes the requested index and the actual argument count.","triggerScenarios":"Calling args.Get(2) on an Arguments slice with fewer than 3 elements; calling args.Int(1) when the mock was set up with only one argument; indexing into args inside a .Run(func(args mock.Arguments) {...}) callback without checking length.","commonSituations":"Mock signature drift — the production method gained/lost a parameter and the test's index-based extraction wasn't updated; off-by-one errors when extracting the Nth argument.","solutions":["Check len(args) before indexing: if len(args) > idx { v = args.Get(idx) }.","Update the Mock.On(...) argument list to match the current method signature so the index is valid.","Prefer named-field extraction by unmarshaling args into a struct, or use args[index] guards in Run callbacks."],"exampleFix":"// before\nm.On(\"Save\", \"x\", \"y\").Run(func(args mock.Arguments) {\n    name := args.String(2) // panic if only 2 args\n})\n// after\nm.On(\"Save\", \"x\", \"y\", \"z\").Run(func(args mock.Arguments) {\n    if len(args) > 2 { name := args.String(2) }\n})","handlingStrategy":"validation","validationCode":"func hasArgAt(args mock.Arguments, i int) bool {\n    return i >= 0 && i < len(args)\n}\n// before args.Get(i): if hasArgAt(args, i) { v = args.Get(i) }","typeGuard":"func argIndexValid(args mock.Arguments, i int) bool {\n    return i >= 0 && i < len(args)\n}","tryCatchPattern":"// Bounds-check before extraction in callbacks:\nm.On(\"M\").Run(func(args mock.Arguments) {\n    if len(args) > 2 {\n        _ = args.Get(2)\n    }\n})","preventionTips":["Always check len(args) before indexing in Run callbacks.","Keep Mock.On argument counts in sync with the real method signature.","Write a helper that extracts by index with a fallback."],"tags":["mock","panic","arguments","index-out-of-range"],"analyzedSha":"001eb7946baf451879253643e4ce4b38eaa0d4a7","analyzedAt":"2026-08-04T21:49:58.715Z","schemaVersion":2}