Skip to content

ktsivkov/funcmock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MockFunc - Overview and Disclaimer

Build Status Go Report Card PkgGoDev

This is a wrapper around github.com/stretchr/testify package.

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.

Installation

go get github.com/ktsivkov/funcmock

Usage

Using a concrete type

package 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)
}

Using an assumed type

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)
}

About

Function mocker wrapping the stretchr/testify API

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages