stretchr/testify · error
assert: arguments: Cannot call Get(%d) because there are %d
Error message
assert: arguments: Cannot call Get(%d) because there are %d argument(s).
What it means
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.
Source
Thrown at mock/mock.go:940
fnType := reflect.TypeOf(fn)
if fnType.Kind() != reflect.Func {
panic(fmt.Sprintf("assert: arguments: %s is not a func", fn))
}
if fnType.NumIn() != 1 {
panic(fmt.Sprintf("assert: arguments: %s does not take exactly one argument", fn))
}
if fnType.NumOut() != 1 || fnType.Out(0).Kind() != reflect.Bool {
panic(fmt.Sprintf("assert: arguments: %s does not return a bool", fn))
}
return argumentMatcher{fn: reflect.ValueOf(fn)}
}
// Get Returns the argument at the specified index.
func (args Arguments) Get(index int) interface{} {
if index+1 > len(args) {
panic(fmt.Sprintf("assert: arguments: Cannot call Get(%d) because there are %d argument(s).", index, len(args)))
}
return args[index]
}
// Is gets whether the objects match the arguments specified.
func (args Arguments) Is(objects ...interface{}) bool {
for i, obj := range args {
if obj != objects[i] {
return false
}
}
return true
}
// Diff gets a string describing the differences between the arguments
// and the specified objects.
//
// Returns the diff string and number of differences found.View on GitHub (pinned to 001eb7946b)
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.
Example fix
// before
m.On("Save", "x", "y").Run(func(args mock.Arguments) {
name := args.String(2) // panic if only 2 args
})
// after
m.On("Save", "x", "y", "z").Run(func(args mock.Arguments) {
if len(args) > 2 { name := args.String(2) }
}) Defensive patterns
Strategy: validation
Validate before calling
func hasArgAt(args mock.Arguments, i int) bool {
return i >= 0 && i < len(args)
}
// before args.Get(i): if hasArgAt(args, i) { v = args.Get(i) } Type guard
func argIndexValid(args mock.Arguments, i int) bool {
return i >= 0 && i < len(args)
} Try / catch
// Bounds-check before extraction in callbacks:
m.On("M").Run(func(args mock.Arguments) {
if len(args) > 2 {
_ = args.Get(2)
}
}) Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- assert: arguments: String(%d) failed because object wasn't c
- 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
- cannot use Func in expectations. Use mock.AnythingOfType("%T
AI-assisted analysis of stretchr/testify@001eb7946b (2026-08-04).
Data as JSON: /data/errors/c3159b58e495edee.json.
Report an issue: GitHub.