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

Mock Client #431

Closed
estahn opened this issue Mar 14, 2022 · 3 comments
Closed

Mock Client #431

estahn opened this issue Mar 14, 2022 · 3 comments

Comments

@estahn
Copy link

estahn commented Mar 14, 2022

As described in #148, a Client interface containing all available methods would be quite useful to add mocking abilities. In the current state, the HTTP client would need to be mocked, which would effectively test the PagerDuty library itself unnecessarily.

Here is an example from the AWS SDK: https://docs.aws.amazon.com/sdk-for-go/api/service/ecr/ecriface/

Example:

type mockPagerDutyClient struct {
	mock.Mock
	pagerduty.ClientAPI
}

func (m *mockPagerDutyClient) ListAbilities() (*ListAbilityResponse, error) {
	return &pagerduty.ListAbilityResponse{}, nil
}

...

pagerDutyClient := new(mockPagerDutyClient)
pagerDutyClient.On("ListAbilities", &pagerduty.ListAbilityResponse{}).Return(mock.Anything)

I could provide a PR. Thoughts?

@theckman
Copy link
Collaborator

Thank you for reaching out.

In Go, it's considered more idiomatic for consumers of a package to define their own local interface that describes what functionality they use. You then write your own minimal mock that implements only those methods, and provides the controls you need to mock responses and errors.

If you take a look at the v2 Go AWS SDK, they stopped offering these features as part of the rewrite because of these idioms. I'm in strong alignment with this decision, because doing this is additional API surface area we'd need to support.

So my initial thought is that we would not want to pursue adding this to this project.

@estahn
Copy link
Author

estahn commented Mar 21, 2022

@theckman Thanks for your thorough response.

There is certainly an advantage to define your own interface as it would lower breaking changes from upstream. On the contrary I would argue it’s repetitive and produced more unnecessary boilerplate.

Using the ECR API Interface was a pleasant experience and I’m surprised it was removed.

Do you possibly have some more reading on idiomatic go that supports this? Idiomatic code seems to be the knockout argument for any discussion.

@theckman
Copy link
Collaborator

@estahn Avoiding boilerplate definitely isn't something that the Go language really concerns itself with. In some ways, it actually encourages it based on two different Go Proverbs:

  • A little copying is better than a little dependency.
  • Clear is better than clever.

For this specific topic on where interfaces are defined, the canonical place I've seen it mentioned is in the CodeReviewComments wiki page on the official Go repo on GitHub: https://github.com/golang/go/wiki/CodeReviewComments#interfaces

@estahn estahn closed this as completed Mar 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants