go-redis / redismock

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

Mocking Multiple Values Fails Randomly #73

Open graytonio opened 1 year ago

graytonio commented 1 year ago

When mocking commands like XAdd with multiple values the test will fail randomly depending on the order of the values the mock and client decide to put them in. Is there a way to validate that these commands have the right values without caring about the order?

Example:

func DemoFunc(db *redis.Client) (string, error) {
    resp := db.XAdd(&redis.XAddArgs{
        Stream: "foo",
        ID: "*",
        Values: map[string]interface{}{
            "foo": "bar",
            "test": "baz",
        },
    })
}

func TestDemoFunc(t *testing.T) {
    db, mock := redismock.NewClientMock()

    mock.ExpectXAdd(&redis.XAddArgs{
        Stream: "foo",
        ID: "*",
        Values: map[string]interface{}{
            "foo": "bar",
            "test": "baz",
        },
    })

    got, err := DemoFunc(db)

    if err := mock.ExpectationsWereMet(); err != nil {
        t.Error(err)
    }
}

This code will randomly either pass or fail.

squat commented 3 months ago

I needed to figure this out today. For my use case it was sufficient to set mock.MatchExpectationsInOrder(false) as in the example: https://github.com/go-redis/redismock/blob/master/example/example.go#L72-L74

benjfield commented 2 months ago

I have this problem too - this is a problem for any unordered queries i.e coming from a map.

Unless I have it wrong, that flag only allows you to run the redis commands out of order, however in a single command with unsorted data (in my immediate case SINTER), it will not fix the problem.

I would appreciate any suggestions if anyone has one.