{"id":"3a333cba1c6e4454","repo":"stretchr/testify","slug":"assert-arguments-string-d-failed-because-objec","errorCode":null,"errorMessage":"assert: arguments: String(%d) failed because object wasn't correct type: %s","messagePattern":"assert: arguments: String\\((.+?)\\) failed because object wasn't correct type: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mock/mock.go","lineNumber":1103,"sourceCode":"// if the argument is of the wrong type.\n//\n// If no index is provided, String() returns a complete string representation\n// of the arguments.\nfunc (args Arguments) String(indexOrNil ...int) string {\n\tif len(indexOrNil) == 0 {\n\t\t// normal String() method - return a string representation of the args\n\t\tvar argsStr []string\n\t\tfor _, arg := range args {\n\t\t\targsStr = append(argsStr, fmt.Sprintf(\"%T\", arg)) // handles nil nicely\n\t\t}\n\t\treturn strings.Join(argsStr, \",\")\n\t} else if len(indexOrNil) == 1 {\n\t\t// Index has been specified - get the argument at that index\n\t\tindex := indexOrNil[0]\n\t\tvar s string\n\t\tvar ok bool\n\t\tif s, ok = args.Get(index).(string); !ok {\n\t\t\tpanic(fmt.Sprintf(\"assert: arguments: String(%d) failed because object wasn't correct type: %s\", index, args.Get(index)))\n\t\t}\n\t\treturn s\n\t}\n\n\tpanic(fmt.Sprintf(\"assert: arguments: Wrong number of arguments passed to String.  Must be 0 or 1, not %d\", len(indexOrNil)))\n}\n\n// Int gets the argument at the specified index. Panics if there is no argument, or\n// if the argument is of the wrong type.\nfunc (args Arguments) Int(index int) int {\n\tvar s int\n\tvar ok bool\n\tif s, ok = args.Get(index).(int); !ok {\n\t\tpanic(fmt.Sprintf(\"assert: arguments: Int(%d) failed because object wasn't correct type: %v\", index, args.Get(index)))\n\t}\n\treturn s\n}\n","sourceCodeStart":1085,"sourceCodeEnd":1121,"githubUrl":"https://github.com/stretchr/testify/blob/001eb7946baf451879253643e4ce4b38eaa0d4a7/mock/mock.go#L1085-L1121","documentation":"Panicked by Arguments.String(index) (mock/mock.go:1089) when args.Get(index) does not type-assert to string. String(indexOrNil...) with one int argument extracts and casts the argument at that position; a non-string value (int, struct, []byte) triggers the panic. The message prints the offending value.","triggerScenarios":"Calling args.String(0) inside a Run/Return callback when the method's first argument is not a Go string (e.g. it's a custom stringer type, []byte, or int).","commonSituations":"Method signature changed so a positional argument is no longer a plain string; using String on a typed string alias (type MyStr string) which does not type-assert to builtin string.","solutions":["Use the correct typed accessor: args.Int, args.Bool, or a manual type switch for non-string types.","If the value is a string alias or fmt.Stringer, convert explicitly: string(args.Get(i).(MyStr)) or args.Get(i).(fmt.Stringer).String().","Update the assertion to reflect the actual argument type after a signature change."],"exampleFix":"// before\nname := args.String(0) // arg is type MyStr\n// after\nname := string(args.Get(0).(MyStr))","handlingStrategy":"type-guard","validationCode":"func argIsString(args mock.Arguments, i int) bool {\n    if i >= len(args) { return false }\n    _, ok := args.Get(i).(string)\n    return ok\n}\n// before args.String(i): if !argIsString(args, i) { t.Fatal(\"arg not string\") }","typeGuard":"func argIsString(args mock.Arguments, i int) bool {\n    if i >= len(args) { return false }\n    _, ok := args.Get(i).(string)\n    return ok\n}","tryCatchPattern":"// Use a type switch instead of direct String(i):\nswitch v := args.Get(i).(type) {\ncase string: name = v\ncase fmt.Stringer: name = v.String()\ndefault: t.Fatalf(\"unexpected type %T\", v)\n}","preventionTips":["Use type switches instead of direct typed accessors for fragile positions.","Keep argument indices documented alongside Mock.On calls.","Update extractors when method signatures change."],"tags":["mock","panic","arguments","type-assertion","string"],"analyzedSha":"001eb7946baf451879253643e4ce4b38eaa0d4a7","analyzedAt":"2026-08-04T21:49:58.715Z","schemaVersion":2}