matryer / moq

Interface mocking tool for go generate
http://bit.ly/meetmoq
MIT License
1.96k stars 126 forks source link

Generate a struct to assert Calls #217

Open joseboretto opened 4 months ago

joseboretto commented 4 months ago

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

For Go templates, the commonly used file extensions are:

  • .gohtml:

https://www.jetbrains.com/help/idea/integration-with-go-templates.html