如何在Go中模拟Redis连接

I am using https://github.com/go-redis/redis package to make Redis DB calls. For unit testing I want to mock this calls, is there any mock library or way to do it?

Thank you all for the response. I found this package https://github.com/alicebob/miniredis very useful for redis mocking.

as @Motakjuq said, create an interface like this

type DB interface {
   GetData(key string) (value string, error)
   SetData(key string, value string) error
}

and implement it with actual redis client (like this) in your code and miniredis in tests.

It's even easier to mock using miniredis than is apparent at first glance. You don't need to mock every function like Get, Set, ZAdd etc. You can just start miniredis and inject its address to the actual client being used in code (e.g. go-redis) this way:

mr := miniredis.Run()
redis.NewClient(&redis.Options{
            Addr: mr.Addr(),
        })

No further mocking would be required. This also enables you to seamlessly use Pipelined(), TxPipelined() etc. even though miniredis doesn't explicitly expose these methods.