{"id":"614b5700ea633ac6","repo":"stretchr/testify","slug":"assert-arguments-bool-d-failed-because-object","errorCode":null,"errorMessage":"assert: arguments: Bool(%d) failed because object wasn't correct type: %v","messagePattern":"assert: arguments: Bool\\((.+?)\\) failed because object wasn't correct type: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mock/mock.go","lineNumber":1143,"sourceCode":"\tobj := args.Get(index)\n\tvar s error\n\tvar ok bool\n\tif obj == nil {\n\t\treturn nil\n\t}\n\tif s, ok = obj.(error); !ok {\n\t\tpanic(fmt.Sprintf(\"assert: arguments: Error(%d) failed because object wasn't correct type: %v\", index, obj))\n\t}\n\treturn s\n}\n\n// Bool 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) Bool(index int) bool {\n\tvar s bool\n\tvar ok bool\n\tif s, ok = args.Get(index).(bool); !ok {\n\t\tpanic(fmt.Sprintf(\"assert: arguments: Bool(%d) failed because object wasn't correct type: %v\", index, args.Get(index)))\n\t}\n\treturn s\n}\n\n// safeTypeName returns the reflect.Type's name without causing a panic.\n// If the provided reflect.Type is nil, it returns the placeholder string \"<nil>\"\nfunc safeTypeName(t reflect.Type) string {\n\tif t == nil {\n\t\treturn \"<nil>\"\n\t}\n\treturn t.Name()\n}\n\nfunc typeAndKind(v interface{}) (reflect.Type, reflect.Kind) {\n\tt := reflect.TypeOf(v)\n\tk := t.Kind()\n\n\tif k == reflect.Ptr {","sourceCodeStart":1125,"sourceCodeEnd":1161,"githubUrl":"https://github.com/stretchr/testify/blob/001eb7946baf451879253643e4ce4b38eaa0d4a7/mock/mock.go#L1125-L1161","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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."],"exampleFix":"// before\nm.On(\"IsEnabled\", ctx).Return(\"yes\", nil)\n...\nif m.IsEnabled(ctx) { ... }   // inside mock: return args.Bool(0), args.Error(1)\n\n// after\nm.On(\"IsEnabled\", ctx).Return(true, nil)\nreturn args.Bool(0), args.Error(1)","handlingStrategy":"validation","validationCode":"// Validate type before reading, so a contract drift logs instead of panicking.\nfunc safeBool(args mock.Arguments, i int) (bool, error) {\n    v := args.Get(i)\n    b, ok := v.(bool)\n    if !ok {\n        return false, fmt.Errorf(\"args[%d] is %T, not bool\", i, v)\n    }\n    return b, nil\n}\n\n// usage inside a mocked method:\n//   enabled, err := safeBool(args, 0)\n//   if err != nil { /* log or fail the test explicitly */ }","typeGuard":"// For a struct/iface that may carry the bool, narrow before asserting.\nfunc isBool(v interface{}) bool {\n    _, ok := v.(bool)\n    return ok\n}\n\n// usage: if !isBool(args.Get(2)) { t.Fatalf(\"expected bool at index 2, got %T\", args.Get(2)) }","tryCatchPattern":"// Go has no try/catch; use defer/recover only at test-helper boundaries,\n// not for control flow.\nfunc recoverBool(t *testing.T, args mock.Arguments, i int) (b bool) {\n    defer func() {\n        if r := recover(); r != nil {\n            t.Fatalf(\"Bool(%d) panicked: %v\", i, r)\n        }\n    }()\n    return args.Bool(i)\n}","preventionTips":["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."],"tags":["testify","mock","type-assertion","panic","go"],"analyzedSha":"001eb7946baf451879253643e4ce4b38eaa0d4a7","analyzedAt":"2026-08-04T21:49:58.715Z","schemaVersion":2}