stretchr/testify · error
assert: arguments: Bool(%d) failed because object wasn't cor
Error message
assert: arguments: Bool(%d) failed because object wasn't correct type: %v
What it means
testify's mock.Arguments.Bool(index) is a strongly-typed getter that performs an unguarded type assertion on the argument captured at the given index (mock/mock.go:1139-1146). It panics rather than returning an error because mock setups are static and programmer-controlled: a type mismatch means the test author wired the mock's return/value list differently than the production code reads it. The panic surfaces immediately at the call site so the contract violation is impossible to miss.
Source
Thrown at mock/mock.go:1143
obj := args.Get(index)
var s error
var ok bool
if obj == nil {
return nil
}
if s, ok = obj.(error); !ok {
panic(fmt.Sprintf("assert: arguments: Error(%d) failed because object wasn't correct type: %v", index, obj))
}
return s
}
// Bool 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) Bool(index int) bool {
var s bool
var ok bool
if s, ok = args.Get(index).(bool); !ok {
panic(fmt.Sprintf("assert: arguments: Bool(%d) failed because object wasn't correct type: %v", index, args.Get(index)))
}
return s
}
// safeTypeName returns the reflect.Type's name without causing a panic.
// If the provided reflect.Type is nil, it returns the placeholder string "<nil>"
func safeTypeName(t reflect.Type) string {
if t == nil {
return "<nil>"
}
return t.Name()
}
func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) {
t := reflect.TypeOf(v)
k := t.Kind()
if k == reflect.Ptr {View on GitHub (pinned to 001eb7946b)
Solutions
- Open the mocked method's signature and confirm which return position is bool; align the .Return(...) argument order so the bool lands at the same index your code reads with args.Bool(i).
- Recompute the index passed to Bool: it is positional into the .Return(...) list, zero-based, not into the input arguments of the mocked call.
- If the value may legitimately be nil or a non-bool, switch to args.Get(i) and do a typed nil-check + assertion yourself instead of Bool(i).
- Add an argument matcher via MatchedBy or .Run to log the actual captured arguments before reading them, so the offending type is visible in test output.
Example fix
// before
m.On("IsEnabled", ctx).Return("yes", nil)
...
if m.IsEnabled(ctx) { ... } // inside mock: return args.Bool(0), args.Error(1)
// after
m.On("IsEnabled", ctx).Return(true, nil)
return args.Bool(0), args.Error(1) Defensive patterns
Strategy: validation
Validate before calling
// Validate type before reading, so a contract drift logs instead of panicking.
func safeBool(args mock.Arguments, i int) (bool, error) {
v := args.Get(i)
b, ok := v.(bool)
if !ok {
return false, fmt.Errorf("args[%d] is %T, not bool", i, v)
}
return b, nil
}
// usage inside a mocked method:
// enabled, err := safeBool(args, 0)
// if err != nil { /* log or fail the test explicitly */ } Type guard
// For a struct/iface that may carry the bool, narrow before asserting.
func isBool(v interface{}) bool {
_, ok := v.(bool)
return ok
}
// usage: if !isBool(args.Get(2)) { t.Fatalf("expected bool at index 2, got %T", args.Get(2)) } Try / catch
// Go has no try/catch; use defer/recover only at test-helper boundaries,
// not for control flow.
func recoverBool(t *testing.T, args mock.Arguments, i int) (b bool) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("Bool(%d) panicked: %v", i, r)
}
}()
return args.Bool(i)
} Prevention
- Treat the .Return(...) argument list as the single source of truth for index ordering; re-derive Bool/Int/Error indices from it whenever the mocked signature changes.
- Keep one .Return per On(...) call and spell out every return value positionally — avoid relying on defaults that can shift indices.
- When stubbing func() (T, error), always pass both values (e.g. .Return(true, nil)); a missing trailing nil is a common cause of shifted or mistyped slots.
- Run the affected test in isolation first (go test -run) so the panic stack points straight at the offending mock setup.
When it happens
Trigger: Calling args.Bool(i) on an Arguments slice (returned by Mock.Called) where element i is not a bool. Concretely: (a) the stub's .Return(...) lists a non-bool (e.g. int, string, *struct, nil) at the position the production code reads with Bool; (b) the index passed to Bool points at the wrong slot after .Return ordering changed; (c) an argument matcher (MatchedBy) or variadic call shifted indices so the bool is no longer where expected; (d) the production method signature changed and now passes a non-bool at that position.
Common situations: Refactoring the mocked method's return tuple without updating the test's .Return(...) values; copy-pasting a mock setup from a similar method whose return order differs; upgrading a dependency whose interface now returns error as the last value, shifting the bool index; passing literal 1/0 ints where a bool was expected; nil returns from a func() bool stub declared as Return(nil).
Related errors
- assert: arguments: String(%d) failed because object wasn't c
- assert: arguments: Int(%d) failed because object wasn't corr
- assert: arguments: Error(%d) failed because object wasn't co
- 'Require' must not be called before 'Run' or 'SetT'
- 'Assert' must not be called before 'Run' or 'SetT'
AI-assisted analysis of stretchr/testify@001eb7946b (2026-08-04).
Data as JSON: /data/errors/614b5700ea633ac6.json.
Report an issue: GitHub.