go-redis / redismock

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

Can ForEachMaster be mocked/expected ? #14

Open meximonster opened 3 years ago

meximonster commented 3 years ago

Hello!

I am not able to test a function that uses the ForEachMaster method. The error returned is "redis: cluster has no nodes" and i cannot add nodes to the redis.ClusterClient created by NewClusterMock() because it takes no arguments. Am i missing something ?

monkey92t commented 3 years ago

Yes, some functions or commands are not supported, I plan to optimize it in the near future.

meximonster commented 3 years ago

Thank you!

ChristophBleyer commented 3 years ago

Hey, I am facing the same Problem. @monkey92t any thoughts / direction on how this might be implemented? I would like to help out here.

meximonster commented 2 years ago

Hello @monkey92t,

Does this apply to the Do command as well ? It cannot be mocked/expected ?

mnlprz commented 1 year ago

same here

guishoudaoge commented 1 year ago

same here

I found a way to do this

        db2, kvsMocker2 := redismock.NewClientMock()
    var temp *red.ClusterClient
    p := gomonkey.ApplyMethod(reflect.TypeOf(temp), "ForEachMaster", func(_ *red.ClusterClient, ctx context.Context, fn func(ctx context.Context, client *red.Client) error) error {
        return fn(ctx, db2)
    })
    defer p.Reset()

    kvsMocker2.ExpectScan(uint64(0), "ut-test", 100).SetErr(fmt.Errorf("Mock REDIS SCAN() return error"))