stretchr/testify · error
assert: arguments: String(%d) failed because object wasn't c
Error message
assert: arguments: String(%d) failed because object wasn't correct type: %s
What it means
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.
Source
Thrown at mock/mock.go:1103
// if the argument is of the wrong type.
//
// If no index is provided, String() returns a complete string representation
// of the arguments.
func (args Arguments) String(indexOrNil ...int) string {
if len(indexOrNil) == 0 {
// normal String() method - return a string representation of the args
var argsStr []string
for _, arg := range args {
argsStr = append(argsStr, fmt.Sprintf("%T", arg)) // handles nil nicely
}
return strings.Join(argsStr, ",")
} else if len(indexOrNil) == 1 {
// Index has been specified - get the argument at that index
index := indexOrNil[0]
var s string
var ok bool
if s, ok = args.Get(index).(string); !ok {
panic(fmt.Sprintf("assert: arguments: String(%d) failed because object wasn't correct type: %s", index, args.Get(index)))
}
return s
}
panic(fmt.Sprintf("assert: arguments: Wrong number of arguments passed to String. Must be 0 or 1, not %d", len(indexOrNil)))
}
// Int gets the argument at the specified index. Panics if there is no argument, or
// if the argument is of the wrong type.
func (args Arguments) Int(index int) int {
var s int
var ok bool
if s, ok = args.Get(index).(int); !ok {
panic(fmt.Sprintf("assert: arguments: Int(%d) failed because object wasn't correct type: %v", index, args.Get(index)))
}
return s
}
View on GitHub (pinned to 001eb7946b)
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.
Example fix
// before name := args.String(0) // arg is type MyStr // after name := string(args.Get(0).(MyStr))
Defensive patterns
Strategy: type-guard
Validate before calling
func argIsString(args mock.Arguments, i int) bool {
if i >= len(args) { return false }
_, ok := args.Get(i).(string)
return ok
}
// before args.String(i): if !argIsString(args, i) { t.Fatal("arg not string") } Type guard
func argIsString(args mock.Arguments, i int) bool {
if i >= len(args) { return false }
_, ok := args.Get(i).(string)
return ok
} Try / catch
// Use a type switch instead of direct String(i):
switch v := args.Get(i).(type) {
case string: name = v
case fmt.Stringer: name = v.String()
default: t.Fatalf("unexpected type %T", v)
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- assert: arguments: Wrong number of arguments passed to Strin
- assert: arguments: Int(%d) failed because object wasn't corr
- assert: arguments: Error(%d) failed because object wasn't co
- assert: arguments: Cannot call Get(%d) because there are %d
- assert: arguments: Bool(%d) failed because object wasn't cor
AI-assisted analysis of stretchr/testify@001eb7946b (2026-08-04).
Data as JSON: /data/errors/3a333cba1c6e4454.json.
Report an issue: GitHub.