skip to content
Alvin Lucillo

Custom predicate on expected input

/ 2 min read

When you want to perform a custom check on the expected input, you can use custom predicate. This is useful when a particular expectation should not be an equality check alone. In the example below, PublishOne is expected to publish a slice with only one non-empty item. The unit test uses mock.MatchedBy that accepts a function with the actual argument, which in this case is batch. The function returns a true if the expectation is met. In the function body, we are checking that the batch argument’s length is 1 and the item is non-empty.

main.go

//go:generate mockery --name=Publisher --dir=. --output=./mocks --outpkg=servicemock --case=snake

// ...

type Publisher interface {
	PublishBatch(ctx context.Context, batch []string) error
}

func PublishOne(ctx context.Context, pub Publisher, item string) error {
	if item == "" {
		return fmt.Errorf("item must be non-empty")
	}
	return pub.PublishBatch(ctx, []string{item})
}

main_test.go

func TestPublishOne(t *testing.T) {
	ctx := context.Background()

	tests := []struct {
		name string
		item string
	}{
		{name: "non_empty", item: "hello"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			mockPub := servicemock.NewPublisher(t)
			mockPub.On(
				"PublishBatch",
				mock.Anything,
				mock.MatchedBy(func(batch []string) bool {
					return len(batch) == 1 && batch[0] != ""
				}),
			).Return(nil).Once()

			err := PublishOne(ctx, mockPub, tt.item)
			require.NoError(t, err)
		})
	}
}

Mocked service below:

// Code generated by mockery v2.53.5. DO NOT EDIT.

package servicemock

import (
	context "context"

	mock "github.com/stretchr/testify/mock"
)

// Publisher is an autogenerated mock type for the Publisher type
type Publisher struct {
	mock.Mock
}

// PublishBatch provides a mock function with given fields: ctx, batch
func (_m *Publisher) PublishBatch(ctx context.Context, batch []string) error {
	ret := _m.Called(ctx, batch)

	if len(ret) == 0 {
		panic("no return value specified for PublishBatch")
	}

	var r0 error
	if rf, ok := ret.Get(0).(func(context.Context, []string) error); ok {
		r0 = rf(ctx, batch)
	} else {
		r0 = ret.Error(0)
	}

	return r0
}

// NewPublisher creates a new instance of Publisher. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewPublisher(t interface {
	mock.TestingT
	Cleanup(func())
}) *Publisher {
	mock := &Publisher{}
	mock.Mock.Test(t)

	t.Cleanup(func() { mock.AssertExpectations(t) })

	return mock
}