-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 Create repo/issue calls #1181
Comments
"Proto" as in gRPC "protobuf"? |
hey again, ah you're right proto files are for gRPC only. Never mind.. I'll close this issue. |
Glen, perhaps you'd be able to help.
which works as it is supposed to, and creates an issue in the defined repo, however what I'm trying to accomplish is to mock out client.Issues.Create(). |
Yes, Go makes it relatively straightforward to mock functions using interfaces. Here is a super-simplified example that will hopefully get you started... please let me know if you have any questions. First, the package main
import (
"context"
"github.com/davecgh/go-spew/spew"
"github.com/google/go-github/github"
)
type issueCreator interface {
Create(ctx context.Context, owner string, repo string, issue *github.IssueRequest) (*github.Issue, *github.Response, error)
}
type issuesClient struct {
ctx context.Context
issuesClient issueCreator
}
func main() {
ctx := context.Background()
client := github.NewClient(nil)
ic := &issuesClient{ctx: ctx, issuesClient: client.Issues}
ic.UnderTest()
}
func (ic *issuesClient) UnderTest() error {
labels := []string{"label1"}
req := &github.IssueRequest{
Title: github.String("title"),
Body: github.String("body"),
Assignee: github.String("assignee"),
Labels: &labels,
}
issue, _, err := ic.issuesClient.Create(ic.ctx, "owner", "repository", req)
if err != nil {
return err
}
spew.Dump(issue)
return nil
} Next, here is the package main
import (
"context"
"testing"
"github.com/google/go-github/github"
)
type mockClient struct {
resp *github.Issue
}
func (m *mockClient) Create(ctx context.Context, owner, repo string, issue *github.IssueRequest) (*github.Issue, *github.Response, error) {
return m.resp, nil, nil
}
func TestUnderTest(t *testing.T) {
ctx := context.Background()
mc := &mockClient{
resp: &github.Issue{
ID: github.Int64(1),
},
}
ic := &issuesClient{ctx: ctx, issuesClient: mc}
if err := ic.UnderTest(); err != nil {
t.Fatalf("UnderTest: %v", err)
}
} I hope that helps. |
@gmlewis you are the man. A million thanks for your thorough and phenomenal response. |
Thank you, @jkalandaibm! |
Hi Folks -
Is there a go-github proto file available anywhere? The benefit of it would be to generate github client files which would be immensely helpful for mocking out requests and running tests locally.
Many thanks
The text was updated successfully, but these errors were encountered: