migueleliasweb / go-github-mock

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

Unable to mock Git.GetRef #24

Closed oba11 closed 2 years ago

oba11 commented 2 years ago

I tried mocking Git.GetRef but unable to make it work

mockedHTTPClient := mock.NewMockedHTTPClient(
    mock.WithRequestMatch(
        mock.GetReposGitRefByOwnerByRepoByRef,
        github.Reference{},
    ),
)
c := github.NewClient(mockedHTTPClient)
ctx := context.Background()

reference, resp, err := c.Git.GetRef(ctx, "foo", "bar", "main")
log.Println(reference)
log.Println(resp.Response.StatusCode)
log.Println(resp.Response.Request)

// time="2022-03-23T12:21:53+04:00" level=info msg="<nil>"
// time="2022-03-23T12:21:53+04:00" level=info msg=404
// time="2022-03-23T12:21:53+04:00" level=info msg="&{GET http://127.0.0.1:55565/repos/foo/bar/git/ref/main HTTP/1.1 1 1 map[Accept:[application/vnd.github.v3+json] User-Agent:[go-github]] <nil> <nil> 0 [] false api.github.com map[] map[] <nil> map[]   <nil> <nil> <nil> 0xc00001c0b0}"

I tried similar mock with c.Git.CreateRef and works fine. Anything I'm doing wrong that should be changed?

migueleliasweb commented 2 years ago

Hi @oba11 , could you try using GetReposGitRefByOwnerByRepoByRef instead?

By looking inside the go-github GetRef() definitions, looks like it's requesting "repos/%v/%v/git/ref/%v". This URL doesn't match PostReposGitRefsByOwnerByRepo but it should work with GetReposGitRefByOwnerByRepoByRef.

:+1:

oba11 commented 2 years ago

@migueleliasweb my apologies, it was a typo which I've corrected. I tried it with GetReposGitRefByOwnerByRepoByRef but didn't seem to work

migueleliasweb commented 2 years ago

I will have a better look soon, @oba11 .

oba11 commented 2 years ago

Thank you @migueleliasweb 🙏

migueleliasweb commented 2 years ago

Hi @oba11 , could you check for me if your problem is similar to this one? https://github.com/migueleliasweb/go-github-mock/issues/26#issuecomment-1080465128

oba11 commented 2 years ago

@migueleliasweb yes you are right, I just tested https://github.com/migueleliasweb/go-github-mock@allow-extended-last-parameters and it works. Thanks alot

migueleliasweb commented 2 years ago

Closing in favour of https://github.com/migueleliasweb/go-github-mock/issues/26

oba11 commented 2 years ago

Thank you