This is a wrapper around
github.com/stretchr/testifypackage.We keep the API as close as possible to the original package.
We only aim to cover the most common use cases of function mocking.
At the moment we support GoLang versions >= 1.22.
go get github.com/ktsivkov/funcmockpackage example
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/ktsivkov/funcmock"
)
type SimpleFunction func(string, string) (string, error)
type Service struct {
simpleFunction SimpleFunction
}
func (s Service) Do() (string, error) {
return s.simpleFunction("a", "b")
}
func TestService(t *testing.T) {
expectedErr := errors.New("test")
expectedOut := "a"
fnBuilder := funcmock.For[SimpleFunction]()
fnBuilder.On("a", "b").Return(expectedOut, expectedErr)
defer fnBuilder.AssertNumberOfCalls(t, 1)
defer fnBuilder.AssertExpectations(t)
fn := fnBuilder.Build()
service := Service{fn}
res, err := service.Do()
assert.ErrorIs(t, err, expectedErr)
assert.Equal(t, expectedOut, res)
}package example
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/ktsivkov/funcmock"
)
type SimpleFunction func(string, string) (string, error)
func MyFunction(a string, b string) (string, error) {
return fmt.Sprintf("%s_%s", a, b), nil
}
type Service struct {
simpleFunction SimpleFunction
}
func (s Service) Do() (string, error) {
return s.simpleFunction("a", "b")
}
func TestService(t *testing.T) {
expectedErr := errors.New("test")
expectedOut := "a"
fnBuilder := funcmock.As(MyFunction)
fnBuilder.On("a", "b").Return(expectedOut, expectedErr)
defer fnBuilder.AssertNumberOfCalls(t, 1)
defer fnBuilder.AssertExpectations(t)
fn := fnBuilder.Build()
service := Service{fn}
res, err := service.Do()
assert.ErrorIs(t, err, expectedErr)
assert.Equal(t, expectedOut, res)
}