Golang Mocking Third Party Lib

Viewed 42

Not sure how to mock and test that the Dispatch function was called (properly)

third party lib (example)


package api

type Client struct {
    httpClient *http.Client
}

func NewClient() (*Client, error) {
    client := &Client{
        httpClient: httpClient,
    }
    return client, nil
}

type Jobs struct {
 client *Client
}

func (c *Client) Jobs() *Jobs {
 return &Jobs{client: c}
}

func (j *Jobs) Dispatch(jobID string) error {
    // method to mock 
}

Tried wrapping the lib creating my own interface (and generating mocks)


package mypackage

import api "github.com/example/api"

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate

//counterfeiter:generate . Client
type Client interface {
    Jobs() *api.Jobs #type from the third party lib
}
package wrapper

type Wrapper struct {
    client mypackage.Client
}

func NewWrapper(client mypackage.Client) Wrapper {
    return Wrapper{
        client: client,
    }
}

func (w *Wrapper) RunTask(name string) (string, error) {
    return n.client.Jobs().Dispatch(name)
}

But the problem still remains, I can't mock the Jobs function because the third-party lib returns the pointer to a struct on the Clients method.

Has someone faced a similar problem and if so, what would be the "correct" way of going about it (maybe its way simpler than what I'm trying to do)

Basically trying to run this test

func TestRunTask(t *testing.T) {
    client := &fakes.FakeClient{}
    nw := NewWrapper(client)
    nw.RunTask("name")
}

and getting the error below which makes sense cause the mocked func Jobs() does not return anything nor can I make it return something that will invalidate using the lib.

=== RUN   TestRunTask
--- FAIL: TestRunTask (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x10094c404]

goroutine 19 [running]:
testing.tRunner.func1.2({0x100a03300, 0x100bdbd90})
    /opt/homebrew/Cellar/go/1.18.4/libexec/src/testing/testing.go:1389 +0x1c8
testing.tRunner.func1()
    /opt/homebrew/Cellar/go/1.18.4/libexec/src/testing/testing.go:1392 +0x384
panic({0x100a03300, 0x100bdbd90})
    /opt/homebrew/Cellar/go/1.18.4/libexec/src/runtime/panic.go:838 +0x204
github.com/hashicorp/nomad/api.(*Jobs).Dispatch(0x0, {0x10094dcb7, 0x4}, 0x0, {0x100c18e48, 0x0, 0x0}, 0x127d15108?)
    /Users/leodias/go/pkg/mod/github.com/hashicorp/nomad/api@v0.0.0-20220919210521-54474a9534ca/jobs.go:441 +0xf4
github.com/1024inc/nomad-tasker/nomad/wrapper.(*NomadWrapper).RunTask(0xe542fbebb5ce?, {0x10094dcb7, 0x4}, 0x632a0f1d?)
    /Users/leodias/workspace/nomad-tasker/nomad/wrapper/wrapper.go:19 +0x74
github.com/1024inc/nomad-tasker/nomad/wrapper.TestRunTask(0x0?)
    /Users/leodias/workspace/nomad-tasker/nomad/wrapper/wrapper_test.go:13 +0x50
testing.tRunner(0x14000107a00, 0x100a49808)
    /opt/homebrew/Cellar/go/1.18.4/libexec/src/testing/testing.go:1439 +0x110
created by testing.(*T).Run
    /opt/homebrew/Cellar/go/1.18.4/libexec/src/testing/testing.go:1486 +0x300


Process finished with the exit code 1
0 Answers
Related