GDev
GDev

Reputation: 478

How do you mock a type within a type in Golang?

The package 'gopkg.in/redis.v3' contains some code

type Client struct {
}

func (*client) Eval (string, []string, []string) *Cmd {
}

type Cmd struct {
}

func (*Cmd) Result () (interface{}, error) {
}

Which works successfully in the following way

func myFunc (cli *redis.Client) {
    result, err := cli.Eval('my script').Result()
}

The problem is that sometimes the Redis cluster gets hammered, has a moment, and the interface returned as a result is nil.

This is reasonably easy to handle but I wish to put a test in place that will ensure that it is actually handled and no type assertion panic occurs.

Traditionally I would insert a mock Redis client into myFunc that can ultimately return nil.

type redisClient interface {
    Eval(string, []string, []string) redisCmd
}

type redisCmd interface {
    Result() (interface{}, error)
}

func myFunc (cli redisClient) {
    result, err := cli.Eval('my script').Result()
}

The problem I am facing is the compiler doesn't recognise that redis.Client satisfies the interface redisClient because it doesn't recognise that the redis.Cmd returned from Eval satisfies redisCmd.

> cannot use client (type *redis.Client) as type redisClient in argument to myFunc:
>    *redis.Client does not implement redisClient (wrong type for Eval method)
>          have Eval(sting, []string, []string) *redis.Cmd
>          want Eval(sting, []string, []string) redisCmd

Upvotes: 1

Views: 1331

Answers (1)

poy
poy

Reputation: 10507

The problem is that your interface does not match the redis client. If you change the interface to:

type redisClient interface {
    Eval(string, []string, []string) *redis.Cmd
}

it will compile. That being said, it looks like you really want rediscmd, so you will need to make a wrapper around the redis client:

type wrapper struct{
  c *redis.Client
}

func (w wrapper) Eval(x sting, y []string, z []string) redisCmd {
  return w.c.Eval(x,y,z) // This assumes that *redis.Cmd implements rediscmd
}

Upvotes: 6

Related Questions