go-redis / redismock

Redis client Mock
https://redis.uptrace.dev
BSD 2-Clause "Simplified" License
272 stars 59 forks source link

Problems On `CustomMatch` and `Regexp` #13

Closed Littledarren closed 3 years ago

Littledarren commented 3 years ago

Sorry to take your precious time to help me.

  1. problem codes

    import "github.com/go-redis/redis/v8" //@v8.11.0
    import "github.com/go-redis/redismock/v8"  // @8.0.6
    // ...
    db, mock := redismock.NewClientMock()
    mock.Regexp().ExpectHSet("somekey", `.*`).SetErr(nil)
    mock.CustomMatch(func(expected, actual []interface{}) error {return nil}).ExpectHSet("somekey", "").SetErr(nil)
  2. What I expect I want the two mock can fulfill all Hsets. No matter what I give them, they should return normally nil.

  3. What I got

parameters do not match expectation '[somekey .*]', but call to cmd '[somekey field1 value1 field2 value2]'

  1. comment

I searched the Internet but found nothing about this two functions, I am not sure whether it is valid to use dot and star in the regexp. So help me please! Feel Free to comment.

monkey92t commented 3 years ago

Under normal circumstances, do not use regular expressions for the key. It may cause errors. Examples of use are as follows:

db.HSet(ctx, "somekey", "field1", "value1", "field2", "value2")

mock.Regexp().ExpectHSet("somekey", "field1", `.*`, "field2", ".*").SetErr(errors.New("test error"))

In addition, you must set an error or return value, such as: Because for redis, it either has an error or returns the result correctly.

mock.Regexp().ExpectHSet("somekey", "field1", `.*`, "field2", ".*").SetErr(errors.New("test error"))
//OR
mock.Regexp().ExpectHSet("somekey", "field1", `.*`, "field2", ".*").SetVal(3)
Littledarren commented 3 years ago

Under normal circumstances, do not use regular expressions for the key. It may cause errors. Examples of use are as follows:

db.HSet(ctx, "somekey", "field1", "value1", "field2", "value2")

mock.Regexp().ExpectHSet("somekey", "field1", `.*`, "field2", ".*").SetErr(errors.New("test error"))

In addition, you must set an error or return value, such as: Because for redis, it either has an error or returns the result correctly.

mock.Regexp().ExpectHSet("somekey", "field1", `.*`, "field2", ".*").SetErr(errors.New("test error"))
//OR
mock.Regexp().ExpectHSet("somekey", "field1", `.*`, "field2", ".*").SetVal(3)

Thanks. In short, it is not feasible to use regexp or custom match to match keys. Thanks for your help!