README
¶
Redis client Mock 
Provide mock test for redis query, Compatible with github.com/go-redis/redis/v8
Install
Confirm that you are using redis.Client the version is github.com/go-redis/redis/v8
go get github.com/airtame/redismock/v8
Quick Start
RedisClient
var ctx = context.TODO()
func NewsInfoForCache(redisDB *redis.Client, newsID int) (info string, err error) {
cacheKey := fmt.Sprintf("news_redis_cache_%d", newsID)
info, err = redisDB.Get(ctx, cacheKey).Result()
if err == redis.Nil {
// info, err = call api()
info = "test"
err = redisDB.Set(ctx, cacheKey, info, 30 * time.Minute).Err()
}
return
}
func TestNewsInfoForCache(t *testing.T) {
db, mock := redismock.NewClientMock()
newsID := 123456789
key := fmt.Sprintf("news_redis_cache_%d", newsID)
// mock ignoring `call api()`
mock.ExpectGet(key).RedisNil()
mock.Regexp().ExpectSet(key, `[a-z]+`, 30 * time.Minute).SetErr(errors.New("FAIL"))
_, err := NewsInfoForCache(db, newsID)
if err == nil || err.Error() != "FAIL" {
t.Error("wrong error")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
}
RedisCluster
clusterClient, clusterMock := redismock.NewClusterMock()
Unsupported Command
RedisClient:
Subscribe
/PSubscribe
RedisCluster
Subscribe
/PSubscribe
Pipeline
/TxPipeline
Watch
DBSize
Documentation
¶
Index ¶
- type ClientMock
- type ClusterClientMock
- type CustomMatch
- type ExpectedBool
- type ExpectedBoolSlice
- type ExpectedClusterSlots
- type ExpectedCmd
- type ExpectedCommandsInfo
- type ExpectedDuration
- type ExpectedError
- type ExpectedFloat
- type ExpectedGeoLocation
- type ExpectedGeoPos
- type ExpectedInt
- type ExpectedIntSlice
- type ExpectedScan
- type ExpectedSlice
- type ExpectedStatus
- type ExpectedString
- type ExpectedStringIntMap
- type ExpectedStringSlice
- type ExpectedStringStringMap
- type ExpectedStringStructMap
- type ExpectedTime
- type ExpectedXInfoGroups
- type ExpectedXInfoStream
- type ExpectedXMessageSlice
- type ExpectedXPending
- type ExpectedXPendingExt
- type ExpectedXStreamSlice
- type ExpectedZSlice
- type ExpectedZWithKey
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ClientMock ¶
type ClientMock interface {
// contains filtered or unexported methods
}
func NewClientMock ¶
func NewClientMock() (*redis.Client, ClientMock)
type ClusterClientMock ¶
type ClusterClientMock interface {
// contains filtered or unexported methods
}
func NewClusterMock ¶
func NewClusterMock() (*redis.ClusterClient, ClusterClientMock)
type CustomMatch ¶
type CustomMatch func(expected, actual []interface{}) error
type ExpectedBool ¶
type ExpectedBool struct {
// contains filtered or unexported fields
}
func (*ExpectedBool) SetVal ¶
func (cmd *ExpectedBool) SetVal(val bool)
type ExpectedBoolSlice ¶
type ExpectedBoolSlice struct {
// contains filtered or unexported fields
}
func (*ExpectedBoolSlice) SetVal ¶
func (cmd *ExpectedBoolSlice) SetVal(val []bool)
type ExpectedClusterSlots ¶
type ExpectedClusterSlots struct {
// contains filtered or unexported fields
}
func (*ExpectedClusterSlots) SetVal ¶
func (cmd *ExpectedClusterSlots) SetVal(val []redis.ClusterSlot)
type ExpectedCmd ¶
type ExpectedCmd struct {
// contains filtered or unexported fields
}
func (*ExpectedCmd) SetVal ¶
func (cmd *ExpectedCmd) SetVal(val interface{})
type ExpectedCommandsInfo ¶
type ExpectedCommandsInfo struct {
// contains filtered or unexported fields
}
func (*ExpectedCommandsInfo) SetVal ¶
func (cmd *ExpectedCommandsInfo) SetVal(val []*redis.CommandInfo)
type ExpectedDuration ¶
type ExpectedDuration struct {
// contains filtered or unexported fields
}
func (*ExpectedDuration) SetVal ¶
func (cmd *ExpectedDuration) SetVal(val time.Duration)
type ExpectedError ¶
type ExpectedError struct {
// contains filtered or unexported fields
}
type ExpectedFloat ¶
type ExpectedFloat struct {
// contains filtered or unexported fields
}
func (*ExpectedFloat) SetVal ¶
func (cmd *ExpectedFloat) SetVal(val float64)
type ExpectedGeoLocation ¶
type ExpectedGeoLocation struct {
// contains filtered or unexported fields
}
func (*ExpectedGeoLocation) SetVal ¶
func (cmd *ExpectedGeoLocation) SetVal(val []redis.GeoLocation)
type ExpectedGeoPos ¶
type ExpectedGeoPos struct {
// contains filtered or unexported fields
}
func (*ExpectedGeoPos) SetVal ¶
func (cmd *ExpectedGeoPos) SetVal(val []*redis.GeoPos)
type ExpectedInt ¶
type ExpectedInt struct {
// contains filtered or unexported fields
}
func (*ExpectedInt) SetVal ¶
func (cmd *ExpectedInt) SetVal(val int64)
type ExpectedIntSlice ¶
type ExpectedIntSlice struct {
// contains filtered or unexported fields
}
func (*ExpectedIntSlice) SetVal ¶
func (cmd *ExpectedIntSlice) SetVal(val []int64)
type ExpectedScan ¶
type ExpectedScan struct {
// contains filtered or unexported fields
}
func (*ExpectedScan) SetVal ¶
func (cmd *ExpectedScan) SetVal(page []string, cursor uint64)
type ExpectedSlice ¶
type ExpectedSlice struct {
// contains filtered or unexported fields
}
func (*ExpectedSlice) SetVal ¶
func (cmd *ExpectedSlice) SetVal(val []interface{})
type ExpectedStatus ¶
type ExpectedStatus struct {
// contains filtered or unexported fields
}
func (*ExpectedStatus) SetVal ¶
func (cmd *ExpectedStatus) SetVal(val string)
type ExpectedString ¶
type ExpectedString struct {
// contains filtered or unexported fields
}
func (*ExpectedString) SetVal ¶
func (cmd *ExpectedString) SetVal(val string)
type ExpectedStringIntMap ¶
type ExpectedStringIntMap struct {
// contains filtered or unexported fields
}
func (*ExpectedStringIntMap) SetVal ¶
func (cmd *ExpectedStringIntMap) SetVal(val map[string]int64)
type ExpectedStringSlice ¶
type ExpectedStringSlice struct {
// contains filtered or unexported fields
}
func (*ExpectedStringSlice) SetVal ¶
func (cmd *ExpectedStringSlice) SetVal(val []string)
type ExpectedStringStringMap ¶
type ExpectedStringStringMap struct {
// contains filtered or unexported fields
}
func (*ExpectedStringStringMap) SetVal ¶
func (cmd *ExpectedStringStringMap) SetVal(val map[string]string)
type ExpectedStringStructMap ¶
type ExpectedStringStructMap struct {
// contains filtered or unexported fields
}
func (*ExpectedStringStructMap) SetVal ¶
func (cmd *ExpectedStringStructMap) SetVal(val []string)
type ExpectedTime ¶
type ExpectedTime struct {
// contains filtered or unexported fields
}
func (*ExpectedTime) SetVal ¶
func (cmd *ExpectedTime) SetVal(val time.Time)
type ExpectedXInfoGroups ¶
type ExpectedXInfoGroups struct {
// contains filtered or unexported fields
}
func (*ExpectedXInfoGroups) SetVal ¶
func (cmd *ExpectedXInfoGroups) SetVal(val []redis.XInfoGroup)
type ExpectedXInfoStream ¶
type ExpectedXInfoStream struct {
// contains filtered or unexported fields
}
func (*ExpectedXInfoStream) SetVal ¶
func (cmd *ExpectedXInfoStream) SetVal(val *redis.XInfoStream)
type ExpectedXMessageSlice ¶
type ExpectedXMessageSlice struct {
// contains filtered or unexported fields
}
func (*ExpectedXMessageSlice) SetVal ¶
func (cmd *ExpectedXMessageSlice) SetVal(val []redis.XMessage)
type ExpectedXPending ¶
type ExpectedXPending struct {
// contains filtered or unexported fields
}
func (*ExpectedXPending) SetVal ¶
func (cmd *ExpectedXPending) SetVal(val *redis.XPending)
type ExpectedXPendingExt ¶
type ExpectedXPendingExt struct {
// contains filtered or unexported fields
}
func (*ExpectedXPendingExt) SetVal ¶
func (cmd *ExpectedXPendingExt) SetVal(val []redis.XPendingExt)
type ExpectedXStreamSlice ¶
type ExpectedXStreamSlice struct {
// contains filtered or unexported fields
}
func (*ExpectedXStreamSlice) SetVal ¶
func (cmd *ExpectedXStreamSlice) SetVal(val []redis.XStream)
type ExpectedZSlice ¶
type ExpectedZSlice struct {
// contains filtered or unexported fields
}
func (*ExpectedZSlice) SetVal ¶
func (cmd *ExpectedZSlice) SetVal(val []redis.Z)
type ExpectedZWithKey ¶
type ExpectedZWithKey struct {
// contains filtered or unexported fields
}
func (*ExpectedZWithKey) SetVal ¶
func (cmd *ExpectedZWithKey) SetVal(val *redis.ZWithKey)
Click to show internal directories.
Click to hide internal directories.