go-redis / redismock

Redis client Mock
https://redis.uptrace.dev
BSD 2-Clause "Simplified" License
271 stars 59 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.