Proposal: mocking function types
- Dominant language
- Go
- Stars
- 2.2k
- Forks
- 135
- Avg merge
- 11d 6h
- Merged PRs (30d)
- 1
Description
**The idea**: Adding the functionality to mock function types.
**The motivation**: Functions are first class objects that are passed into functions/methods and stored in structs. To test that we have the desired behaviour where they are being used it would be useful to be able to mock them in a similar fashion to interfaces.
**What I am asking**: Would you consider adding support for such functionality? See below for an example setup and expected result. N.B. I thought it useful to ask if you would consider it before I attempt to implement a solution.
---
The expectation would be that running `go generate` on
```go
//go:generate moq -out mocks.go . Solver
// Solver represent a function that solves a problem that
// takes a long time.
type Solver func(ctx context.Context, in [][]int) ([]int, error)
```
yields something (along with suitable documentation) similar to
```go
import (
"context"
"sync"
)
var _ Solver = (*SolverMock)(nil).Call
type SolverMock struct {
Func func(ctx context.Context, in [][]int) ([]int, error)
calls []struct {
Ctx context.Context
In [][]int
}
mu sync.RWMutex
}
func (e *SolverMock) Call(ctx context.Context, in [][]int) ([]int, error) {
if e.Func == nil {
panic("SolverMock.Func: method is nil but Call was just called")
}
callInfo := struct {
Ctx context.Context
In [][]int
}{
Ctx: ctx,
In: in,
}
e.mu.Lock()
e.calls = append(e.calls, callInfo)
e.mu.Unlock()
return e.Func(ctx, in)
}
func (e *SolverMock) Calls() []struct {
Ctx context.Context
In [][]int
} {
var calls []struct {
Ctx context.Context
In [][]int
}
e.mu.RLock()
calls = e.calls
e.mu.RUnlock()
return calls
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Begin with the proposed `go generate` invocation and the `Solver` function type example. Review how `moq` currently handles generated mocks, then determine the required generated output and validation for function types. Done means the requested function-type mock is supported with suitable documentation and tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- devtools, testing
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100