diff --git a/package/moq/moq.go b/package/moq/moq.go index 0eff431..b647076 100644 --- a/package/moq/moq.go +++ b/package/moq/moq.go @@ -221,13 +221,22 @@ var moqTemplate = `package {{.PackageName}} // } // // // TODO: use mocked{{.InterfaceName}} in code that requires {{.InterfaceName}} -// +// // and then make assertions. +// // } type {{.InterfaceName}}Mock struct { {{- range .Methods }} // {{.Name}}Func mocks the {{.Name}} method. {{.Name}}Func func({{ .Arglist }}) {{.ReturnArglist}} +{{ end }} + // CallsTo gets counters for each of the methods indicating + // how many times each one was called. + CallsTo struct { +{{- range .Methods }} + // {{ .Name }} holds the number of calls to the {{.Name}} method. + {{ .Name }} uint64 {{- end }} + } } {{ range .Methods }} // {{.Name}} calls {{.Name}}Func. @@ -235,6 +244,7 @@ func (mock *{{$obj.InterfaceName}}Mock) {{.Name}}({{.Arglist}}) {{.ReturnArglist if mock.{{.Name}}Func == nil { panic("moq: {{$obj.InterfaceName}}Mock.{{.Name}}Func is nil but was just called") } + atomic.AddUint64(&mock.CallsTo.{{.Name}}, 1) // count this {{- if .ReturnArglist }} return mock.{{.Name}}Func({{.ArgCallList}}) {{- else }} diff --git a/package/moq/moq_test.go b/package/moq/moq_test.go index 0372756..36ff5b2 100644 --- a/package/moq/moq_test.go +++ b/package/moq/moq_test.go @@ -27,6 +27,9 @@ func TestMoq(t *testing.T) { "func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*Person, error)", "panic(\"moq: PersonStoreMock.CreateFunc is nil but was just called\")", "panic(\"moq: PersonStoreMock.GetFunc is nil but was just called\")", + "CallsTo struct {", + "atomic.AddUint64(&mock.CallsTo.Get, 1)", + "ClearCache uint64", } for _, str := range strs { if !strings.Contains(s, str) {