Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot use redismock.NewClientMock() (value of type *redis.Client) as redis.Cmdable value in assignment #29

Open
nikjoshistori opened this issue Feb 28, 2022 · 0 comments

Comments

@nikjoshistori
Copy link

Hello team I getting below error
cannot use redismock.NewClientMock() (value of type *redis.Client) as redis.Cmdable value in assignment

For the code below
redis.go
`package redis

import (
"fmt"
"time"

"github.com/go-redis/redis"

)

type redisConn struct {
host string
port string
password string
client *redis.Client
}

const (
redisPwd = ""
redisPort = "6379"
)

// RedisNewConn
func (r redisConn) RedisNewConn() error {

r.client = redis.NewClient(&redis.Options{
	Addr:     r.host + ":" + r.port,
	Password: r.password,
})

if r.client == nil {
	return fmt.Errorf("redis Connection failed")
}

if _, err := r.client.Ping().Result(); err != nil {

	return err
}

return nil

}

// Set
func (r redisConn) Set(k string, d string, expiry time.Duration) error {

err := r.client.Set(k, d, expiry).Err()
if err != nil {
	return err
}
return nil

}

// Get
func (r redisConn) Get(k string) (string, error) {

d, e := r.client.Get(k).Result()
if e != nil {
	return "", e
}

return d, nil

}
redis_test.gopackage redis

import (
"testing"

"[github.com/go-redis/redis](http://github.com/go-redis/redis)"
"[github.com/go-redis/redismock/v8](http://github.com/go-redis/redismock/v8)"

)

type mockRedis struct {
key string
val string
client *redis.Client
}

func TestGet(t *testing.T) {

var conn redisConn
var mock redismock.ClientMock
conn.client, mock = redismock.NewClientMock()
mockr := mockRedis{key: "news_redis_cache_123456", val: "s"}
mock.ExpectGet(mockr.key).SetVal(mockr.val)

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant