timehop / apns

A Go package to interface with the Apple Push Notification Service
https://godoc.org/github.com/timehop/apns
MIT License
185 stars 47 forks source link

No mockable client interface #24

Closed willfaught closed 1 year ago

willfaught commented 9 years ago

In order to test code that uses this client, I would have to mock net.Conn, if I understand correctly. This is onerous; mocking the client's API is much easier. Exporting a mockable client interface, and an implementation of it, helps with this.

Something along these lines...

type Client interface {
    Send(n Notification) error
    Conn() *Conn
    FailedNotifs() chan NotificationResult
}

type client struct {[...]} // implements Client

func NewClient(gw string, cert string, key string) (Client, error) {...}

so we can test code like this:

func Foo(client apns.Client) error {
    [...]
    if err := client.Send([...]); err != nil {
        return err
    }
    [...]
}

like this:

func TestFoo(t *testing.T) {
    mymock := MyMock{} // has type apns.Client
    mymock.When("Send", apnsClient.Notification{[...]}).Return(errors.New("test"))
    if Foo(mymock) == nil {
        t.Error("Foo should return apns error")
    }
}
nathany commented 9 years ago

This is something I wouldn't mind either, which I opened #21 for. Ben has been reworking the code a fair bit, so I'm not sure what this will look like.

Go lets us create interfaces for code in other packages, so there may be a way to do this without requiring changes to timehop/apns. I'm not sure, as I haven't given it much thought yet.

willfaught commented 9 years ago

@nathany I wrote my own interface wrapper for apns.Client, but it ought to be unnecessary, and it's so easy to do.

bdotdub commented 9 years ago

Definitely makes sense. In retrospect, testing against a apns.Client struct is def a pain. Happy to merge if someone wants to open a PR