elliotchance / redismock

🕋 Mocking Redis in unit tests in Go.
MIT License
147 stars 24 forks source link

cannot use r (variable of type *redismock.ClientMock) as redis.Cmdable value in argument to RedisIsAvailable #13

Closed hom-bahrani closed 4 years ago

hom-bahrani commented 4 years ago

Firstly thanks for writing this package, and please forgive my ignorance I'm new to Go. In your example newTestRedis() returns a type of *redismock.ClientMock which is saved in the variable r. How can this be passed into the RedisIsAvailable function which is expecting a type of redis.Cmdable.

elliotchance commented 4 years ago

Sure, let me explain.

*redismock.ClientMock is struct. Structs can have instance variables and methods (basically the same thing as a class in most other languages). redis.Cmdable is an interface. Interfaces work the same way as interfaces in other languages (they define a set of methods that must be implemented).

In Go any struct will implement an interface by simply having the all the method signatures in that interface. Unlike other languages that require the class to explicitly say implements redis.Cmdable.

redis.Cmdable is a massive interface, and it grows with each release of the redis package. However, as long as ClientMock has at least all of the methods defined in the interface it can always be used as such.

Does that answer your question?

hom-bahrani commented 4 years ago

thanks @elliotchance thats perfect 👍