moq
moq copied to clipboard
Generate a struct to assert Calls
Problem
When you need to assert the call, you have to copy/duplicate the "Calls" struct.
Example
// When
s.GetPerson(ctx, "1")
// Then
actual := personStore.GetCalls()
expected := []struct {
Ctx context.Context
ID string
}{
{
Ctx: ctx,
ID: "1",
},
}
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("Expected %v but got %v", expected, actual)
}
Solution
Create the "Calls" struct as an independent struct
Example
// When
s.GetPerson(ctx, "1")
// Then
actual := personStore.GetCalls()
expected := []PersonStoreMockGetCalls{
{
Ctx: ctx,
ID: "1",
},
}
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("Expected %v but got %v", expected, actual)
}
Extra
- Move the template to a template file. The extension of the file enables the syntax highlighting of the template.
For Go templates, the commonly used file extensions are:
- .gohtml:
https://www.jetbrains.com/help/idea/integration-with-go-templates.html