redigomock

Easy way to unit test projects using redigo library (Redis client in go). You can find the latest release here.
install
go get -u github.com/rafaeljusto/redigomock
usage
Here is an example of using redigomock, for more information please check the API documentation.
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
"github.com/rafaeljusto/redigomock"
)
type Person struct {
Name string `redis:"name"`
Age int `redis:"age"`
}
func RetrievePerson(conn redis.Conn, id string) (Person, error) {
var person Person
values, err := redis.Values(conn.Do("HGETALL", fmt.Sprintf("person:%s", id)))
if err != nil {
return person, err
}
err = redis.ScanStruct(values, &person)
return person, err
}
func main() {
conn := redigomock.NewConn()
cmd := conn.Command("HGETALL", "person:1").ExpectMap(map[string]string{
"name": "Mr. Johson",
"age": "42",
})
person, err := RetrievePerson(conn, "1")
if err != nil {
fmt.Println(err)
return
}
if conn.Stats(cmd) != 1 {
fmt.Println("Command was not used")
return
}
if person.Name != "Mr. Johson" {
fmt.Printf("Invalid name. Expected 'Mr. Johson' and got '%s'\n", person.Name)
return
}
if person.Age != 42 {
fmt.Printf("Invalid age. Expected '42' and got '%d'\n", person.Age)
return
}
conn.Clear()
cmd = conn.Command("HGETALL", "person:1").ExpectError(fmt.Errorf("Simulate error!"))
person, err = RetrievePerson(conn, "1")
if err == nil {
fmt.Println("Should return an error!")
return
}
if conn.Stats(cmd) != 1 {
fmt.Println("Command was not used")
return
}
fmt.Println("Success!")
}
mocking a subscription
package main
import "github.com/rafaeljusto/redigomock"
func CreateSubscriptionMessage(data []byte) []interface{} {
values := []interface{}{}
values = append(values, interface{}([]byte("message")))
values = append(values, interface{}([]byte("chanName")))
values = append(values, interface{}(data))
return values
}
func main() {
conn := redigomock.NewConn()
values := []interface{}{}
values = append(values, interface{}([]byte("subscribe")))
values = append(values, interface{}([]byte("chanName")))
values = append(values, interface{}([]byte("1")))
conn.Command("SUBSCRIBE", subKey).Expect(values)
conn.ReceiveWait = true
conn.AddSubscriptionMessage(CreateSubscriptionMessage([]byte("hello")))
go func() {
conn.ReceiveNow <- true
conn.ReceiveNow <- true
}()
}
connections pool
conn := redigomock.NewConn()
pool := &redis.Pool{
Dial: func() (redis.Conn, error) { return conn, nil },
MaxIdle: 10,
}
dynamic handling arguments
Sometimes you need to check the executed arguments in your Redis command. For
that you can use the command handler.
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
"github.com/rafaeljusto/redigomock"
)
func Publish(conn redis.Conn, x, y int) error {
if x < 0 {
x = 0
}
if y < 0 {
y = 0
}
_, err := conn.Do("PUBLISH", "sumCh", int64(x+y))
return err
}
func main() {
conn := redigomock.NewConn()
conn.GenericCommand("PUBLISH").Handle(redigomock.ResponseHandler(func(args []interface{}) (interface{}, error) {{
if len(args) != 2 {
return nil, fmt.Errorf("unexpected number of arguments: %d", len(args))
}
v, ok := args[1].(int64)
if !ok {
return nil, fmt.Errorf("unexpected type %T", args[1])
}
if v < 0 {
return nil, fmt.Errorf("unexpected value '%d'", v)
}
return int64(1), nil
})
if err := Publish(conn, -1, 10); err != nil {
fmt.Println(err)
return
}
fmt.Println("Success!")
}