redigomock

Easy way to unit test projects using redigo library (Redis client in go)

  • 所有者: rafaeljusto/redigomock
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

redigomock

Build Status
GoDoc

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() {
	// Simulate command result

	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
	}

	// Simulate command error

	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

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
}

rconnSub := redigomock.NewConn()

// Setup the initial subscription message
values := []interface{}{}
values = append(values, interface{}([]byte("subscribe")))
values = append(values, interface{}([]byte("chanName")))
values = append(values, interface{}([]byte("1")))
cmd := rconnSub.Command("SUBSCRIBE", subKey).Expect(values)
rconnSub.ReceiveWait = true

// Add a response that will come back as a subscription message
rconnSub.AddSubscriptionMessage(CreateSubscriptionMessage([]byte("hello")))

//You need to send messages to rconnSub.ReceiveNow in order to get a response.
//Sending to this channel will block until receive, so do it in a goroutine
go func() {
    rconnSub.ReceiveNow <- true //This unlocks the subscribe message
    rconnSub.ReceiveNow <- true //This sends the "hello" message
}()

connections pool

// Note you cannot get access to the connection via the pool,
// the only way is to use this conn variable.
conn := redigomock.NewConn()
pool := &redis.Pool{
	// Return the same connection mock for each Get() call.
	Dial:    func() (redis.Conn, error) { return conn, nil },
	MaxIdle: 10,
}

主要指标

概览
名称与所有者rafaeljusto/redigomock
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2014-07-11 12:26:27
推送于2025-06-05 10:19:56
最后一次提交2025-06-05 07:18:42
发布数13
最新版本名称v3.1.3 (发布于 2025-06-05 07:19:22)
第一版名称v1.0 (发布于 2015-04-23 08:24:43)
用户参与
星数204
关注者数6
派生数28
提交数172
已启用问题?
问题数34
打开的问题数0
拉请求数36
打开的拉请求数0
关闭的拉请求数4
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?