funcmock

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2025 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder[T any] struct {
	// contains filtered or unexported fields
}

func As added in v1.1.0

func As[T any](_ T) *Builder[T]

func For

func For[T any]() *Builder[T]

func (*Builder[T]) AssertCalled

func (m *Builder[T]) AssertCalled(t mock.TestingT, arguments ...interface{})

func (*Builder[T]) AssertExpectations

func (m *Builder[T]) AssertExpectations(t mock.TestingT)

func (*Builder[T]) AssertNotCalled

func (m *Builder[T]) AssertNotCalled(t mock.TestingT, arguments ...interface{})

func (*Builder[T]) AssertNumberOfCalls

func (m *Builder[T]) AssertNumberOfCalls(t mock.TestingT, expectedCalls int)

func (*Builder[T]) Build

func (m *Builder[T]) Build() T

func (*Builder[T]) Called

func (m *Builder[T]) Called(arguments ...interface{}) mock.Arguments

func (*Builder[T]) IsCallable

func (m *Builder[T]) IsCallable(t mock.TestingT, arguments ...interface{}) bool

func (*Builder[T]) On

func (m *Builder[T]) On(args ...interface{}) *mock.Call

func (*Builder[T]) String

func (m *Builder[T]) String() string

func (*Builder[T]) Test

func (m *Builder[T]) Test(t mock.TestingT)

func (*Builder[T]) TestData

func (m *Builder[T]) TestData() objx.Map

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址