Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate a struct to assert Calls #217

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ The mocked structure implements the interface, where each method calls the assoc
You can run the same command with `-fmt noop` to print the generated source code without attempting to format it.
This can aid in debugging the root cause.

## Development

- This project uses
- https://pkg.go.dev/text/template

## License

The Moq project (and all code) is licensed under the [MIT License](LICENSE).
Expand Down
145 changes: 87 additions & 58 deletions example/mockpersonstore_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions example/usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package example

import "context"

type Service struct {
PersonStore PersonStore
}

func NewService(personStore PersonStore) *Service {
return &Service{
PersonStore: personStore,
}
}

func (s *Service) GetPerson(ctx context.Context, id string) (*Person, error) {
return s.PersonStore.Get(ctx, id)
}
33 changes: 33 additions & 0 deletions example/usage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package example

import (
"context"
"reflect"
"testing"
)

func TestService_GetPerson(t *testing.T) {
ctx := context.Background()
personStore := &PersonStoreMock{
GetFunc: func(ctx context.Context, id string) (*Person, error) {
return &Person{
ID: "1",
Name: "John Doe",
}, nil
},
}
s := NewService(personStore)
// 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)
}
}
Loading