Redis: Define an interface for redis.Client

Created on 28 Feb 2017  路  4Comments  路  Source: go-redis/redis

Would consider defining an interface for redis.Client so it is possible to drop in a mock during testing?

type Client struct {
  baseClient
  cmdable
}

Compare to InfluxDB where client.Client easily can be replaced with just a couple of lines of code:

type dummyClient struct {
}

func (dc *dummyClient) Ping(timeout time.Duration) (time.Duration, string, error) {
    return nil, "", nil
}

func (dc *dummyClient) Write(bp client.BatchPoints) error {
    return nil
}

func (dc *dummyClient) Query(q client.Query) (*client.Response, error) {
    return nil, nil
}

func (dc *dummyClient) Close() error {
    return nil
}

Most helpful comment

All 4 comments

The Cmdable interface is huge - there are almost 200 calls to mock. Any chance that it can be broken up into a couple of smaller interfaces?

Simple, just embed the interface and moc/implement the methods you are using:

type myMockType struct {
  redis.Cmdable
}

func (c *myMockType) Ping() *redis.StatusCommand {
  ...
}

Ah, didn't think of that! I'll try that, and submit a diff to document how to do it...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

qiqisteve picture qiqisteve  路  3Comments

songlong picture songlong  路  8Comments

MaerF0x0 picture MaerF0x0  路  7Comments

chenweiyj picture chenweiyj  路  3Comments

pranas picture pranas  路  8Comments