testify icon indicating copy to clipboard operation
testify copied to clipboard

feat!(mock): allow mock to support func as `Return` param

Open MuhammedIrfan opened this issue 3 years ago • 0 comments

Summary

This PR allows the mock to support func as Return param, which enables to mock to be used for Table Driven Testing.

Changes

Changed Return param to allow function.

func (c *Call) Return(fn func(args Arguments) Arguments) *Call {
	c.lock()
	defer c.unlock()

	c.ReturnArguments = fn

	return c
}

Motivation

While using the mock package with our custom table driven framework we faced a challenge where in we need to return the data from mock storage. By implementing this PR it will enable provide data from mock storage as done in example

Example usage

Mock.On("DoSomething").Return(func(args Arguments) Arguments {
	return []interface{}{errors.New("failed")}
})

Breaking Change

BREAKING CHANGE: `returnArguments ...interface{}` param of MockCall.Return now only accepts `func(args Arguments) Arguments` 

MuhammedIrfan avatar Sep 17 '22 16:09 MuhammedIrfan