migueleliasweb / go-github-mock

A library to aid unittesting code that uses Golang's Github SDK
MIT License
98 stars 24 forks source link

Mock test for function that calls the same api request twice #13

Closed mariojose123 closed 2 years ago

mariojose123 commented 2 years ago

I am having some errors when I created two MockBackendOption with PatchReposPullsByOwnerByRepoByPullNumber for the function NewMockedHTTPClient, its an EOF error , I don't know if it is an error of my code or go-GitHub-mock , if it is my error what should I do instead of that. I am trying to create a function that calls the same GitHub API request twice.

mariojose123 commented 2 years ago

this is my mockedhttpclient `mockedHttpClient := mock.NewMockedHTTPClient(

    mock.WithRequestMatch(
        mock.PatchReposPullsByOwnerByRepoByPullNumber,
        &github.PullRequest{
            ID:                  github.Int64(1),
            Number:              nil,
            State:               github.String("closed"),
            Locked:              nil,
            Title:               nil,
            Body:                nil,
            CreatedAt:           nil,
            UpdatedAt:           nil,
            ClosedAt:            nil,
            MergedAt:            nil,
            Labels:              nil,
            User:                nil,
            Draft:               nil,
            Merged:              nil,
            Mergeable:           nil,
            MergeableState:      nil,
            MergedBy:            nil,
            MergeCommitSHA:      nil,
            Rebaseable:          nil,
            Comments:            nil,
            Commits:             nil,
            Additions:           nil,
            Deletions:           nil,
            ChangedFiles:        nil,
            URL:                 nil,
            HTMLURL:             nil,
            IssueURL:            nil,
            StatusesURL:         nil,
            DiffURL:             nil,
            PatchURL:            nil,
            CommitsURL:          nil,
            CommentsURL:         nil,
            ReviewCommentsURL:   nil,
            ReviewCommentURL:    nil,
            ReviewComments:      nil,
            Assignee:            nil,
            Assignees:           nil,
            Milestone:           nil,
            MaintainerCanModify: nil,
            AuthorAssociation:   nil,
            NodeID:              nil,
            RequestedReviewers:  nil,
            RequestedTeams:      nil,
            Links:               nil,
            Head:                nil,
            Base:                nil,
            ActiveLockReason:    nil,
        },),
        mock.WithRequestMatch(
        mock.PatchReposPullsByOwnerByRepoByPullNumber,
        &github.PullRequest{
            ID:                  github.Int64(1),
            Number:              nil,
            State:               github.String("closed"),
            Locked:              nil,
            Title:               nil,
            Body:                nil,
            CreatedAt:           nil,
            UpdatedAt:           nil,
            ClosedAt:            nil,
            MergedAt:            nil,
            Labels:              nil,
            User:                nil,
            Draft:               nil,
            Merged:              nil,
            Mergeable:           nil,
            MergeableState:      nil,
            MergedBy:            nil,
            MergeCommitSHA:      nil,
            Rebaseable:          nil,
            Comments:            nil,
            Commits:             nil,
            Additions:           nil,
            Deletions:           nil,
            ChangedFiles:        nil,
            URL:                 nil,
            HTMLURL:             nil,
            IssueURL:            nil,
            StatusesURL:         nil,
            DiffURL:             nil,
            PatchURL:            nil,
            CommitsURL:          nil,
            CommentsURL:         nil,
            ReviewCommentsURL:   nil,
            ReviewCommentURL:    nil,
            ReviewComments:      nil,
            Assignee:            nil,
            Assignees:           nil,
            Milestone:           nil,
            MaintainerCanModify: nil,
            AuthorAssociation:   nil,
            NodeID:              nil,
            RequestedReviewers:  nil,
            RequestedTeams:      nil,
            Links:               nil,
            Head:                nil,
            Base:                nil,
            ActiveLockReason:    nil,
        },),

)`

migueleliasweb commented 2 years ago

Hi @mariojose123, the mocks are indexed by their endpoint pattern.

In this case you need to, instead of setting WithRequestMatch twice (each time with a single mock request), you can just set the &github.PullRequest{} parameter twice as mock.With* functions are variadic.

Eg.

mock.WithRequestMatch(
    mock.PatchReposPullsByOwnerByRepoByPullNumber,
    &github.PullRequest{}, // first request to be mocked
    &github.PullRequest{}, //second request to be mocked
    // ... N request to be mocked
)

Let me know if that fixes your problem ;)

mariojose123 commented 2 years ago

yeah it fixed, thank you