stretchr/testify · error

assert: arguments: %s does not return a bool

Error message

assert: arguments: %s does not return a bool

What it means

Panicked by MatchedBy() (mock/mock.go:921) when the passed function does not return exactly one value of kind Bool. MatchedBy interprets a true/false return as match/no-match, so any other signature is invalid. Check at mock/mock.go:930.

Source

Thrown at mock/mock.go:931

//
// Example:
//
//	m.On("Do", MatchedBy(func(req *http.Request) bool { return req.Host == "example.com" }))
//
// fn must be a function accepting a single argument (of the expected type)
// which returns a bool. If fn doesn't match the required signature,
// MatchedBy() panics.
func MatchedBy(fn interface{}) argumentMatcher {
	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

View on GitHub (pinned to 001eb7946b)

Solutions

  1. Make the matcher return a single bool: mock.MatchedBy(func(x T) bool { ... }).
  2. Wrap an error-returning validator: mock.MatchedBy(func(x T) bool { return validator(x) == nil }).
  3. If your check returns (bool, error), collapse it to bool by ignoring or failing on the error inside the matcher.

Example fix

// before
m.On("Do", mock.MatchedBy(func(x int) error { return validate(x) })).Return(nil)
// after
m.On("Do", mock.MatchedBy(func(x int) bool { return validate(x) == nil })).Return(nil)
Defensive patterns

Strategy: validation

Validate before calling

func returnsBool(fn interface{}) bool {
    t := reflect.TypeOf(fn)
    return t != nil && t.Kind() == reflect.Func && t.NumOut() == 1 && t.Out(0).Kind() == reflect.Bool
}
// before MatchedBy: assert.True(t, returnsBool(matcher))

Type guard

func matcherReturnsBool(fn interface{}) bool {
    t := reflect.TypeOf(fn)
    return t != nil && t.Kind() == reflect.Func && t.NumOut() == 1 && t.Out(0).Kind() == reflect.Bool
}

Try / catch

// Validate return signature before constructing:
if !matcherReturnsBool(matcher) {
    log.Fatal("matcher must return a single bool")
}
m.On("Do", mock.MatchedBy(matcher))

Prevention

When it happens

Trigger: Calling mock.MatchedBy(func(x int) {}) (no return), mock.MatchedBy(func(x int) error { ... }) (wrong return type), or mock.MatchedBy(func(x int) (bool, error) { ... }) (two returns).

Common situations: Reusing an existing validation function that returns an error as a matcher; forgetting the bool return after refactoring; copying a predicate that returns a custom Result type.

Related errors


AI-assisted analysis of stretchr/testify@001eb7946b (2026-08-04). Data as JSON: /data/errors/f1639225b2c2af80.json. Report an issue: GitHub.