项目作者: go-redis

项目描述 :
Redis client Mock
高级语言: Go
项目地址: git://github.com/go-redis/redismock.git
创建时间: 2020-12-09T10:56:14Z
项目社区:https://github.com/go-redis/redismock

开源协议:BSD 2-Clause "Simplified" License

下载


Redis client Mock

Provide mock test for redis query, Compatible with github.com/redis/go-redis/v9

Install

Confirm that you are using redis.Client the version is github.com/redis/go-redis/v9

  1. go get github.com/go-redis/redismock/v9

Quick Start

RedisClient

  1. var ctx = context.TODO()
  2. func NewsInfoForCache(redisDB *redis.Client, newsID int) (info string, err error) {
  3. cacheKey := fmt.Sprintf("news_redis_cache_%d", newsID)
  4. info, err = redisDB.Get(ctx, cacheKey).Result()
  5. if err == redis.Nil {
  6. // info, err = call api()
  7. info = "test"
  8. err = redisDB.Set(ctx, cacheKey, info, 30 * time.Minute).Err()
  9. }
  10. return
  11. }
  12. func TestNewsInfoForCache(t *testing.T) {
  13. db, mock := redismock.NewClientMock()
  14. newsID := 123456789
  15. key := fmt.Sprintf("news_redis_cache_%d", newsID)
  16. // mock ignoring `call api()`
  17. mock.ExpectGet(key).RedisNil()
  18. mock.Regexp().ExpectSet(key, `[a-z]+`, 30 * time.Minute).SetErr(errors.New("FAIL"))
  19. _, err := NewsInfoForCache(db, newsID)
  20. if err == nil || err.Error() != "FAIL" {
  21. t.Error("wrong error")
  22. }
  23. if err := mock.ExpectationsWereMet(); err != nil {
  24. t.Error(err)
  25. }
  26. }

RedisCluster

  1. clusterClient, clusterMock := redismock.NewClusterMock()

Unsupported Command

RedisClient:

  • Subscribe / PSubscribe

RedisCluster

  • Subscribe / PSubscribe
  • Pipeline / TxPipeline
  • Watch