elliotchance / redismock

🕋 Mocking Redis in unit tests in Go.
MIT License
147 stars 24 forks source link

Get command does not work as expected. #9

Closed nmakro closed 5 years ago

nmakro commented 5 years ago

Following your example:

`func RedisIsAvailable(client redis.Cmdable) bool { return client.Ping().Err() == nil }

func RedisGet(client redis.Cmdable) string { val, _ := client.Get("mykey").Result() return val }

// Test Redis is down. func TestRedis(t *testing.T) { r := newTestRedis() r.On("Ping"). Return(redis.NewStatusResult("", errors.New("server not available"))) r.On("Get", "mykey").Return(redis.NewStringResult("myvalue", nil))

assert.False(t, RedisIsAvailable(r))
assert.Equal(t, RedisGet(r), "myvalue")

}`

and then i get an error:

Running tool: /usr/local/go/bin/go test -timeout 30s -run ^TestRedis$

--- FAIL: TestRedis (0.00s) panic:

mock: Unexpected Method Call

Get()

The closest call I have is:

Get(string) 0: "mykey"

Provided 1 arguments, mocked for 0 arguments Diff: 0: FAIL: (Missing) != (string=mykey) [recovered] panic:

mock: Unexpected Method Call

Get()

The closest call I have is:

Get(string) 0: "mykey"

Provided 1 arguments, mocked for 0 arguments Diff: 0: FAIL: (Missing) != (string=mykey)

I am using it wrong? I found a way to fix this by passing the key in the Called method as an argument here: https://github.com/elliotchance/redismock/blob/master/cmdable.go#L385 Like this: return m.Called(key).Get(0).(*redis.StringCmd)

I haven't tested other commands that take arguments but maybe the issue will persist for others also,

elliotchance commented 5 years ago

Ah, you're absolutely correct. This is a bug. It would also apply to all of the other functions.

Would you like to put in a PR for it?

nmakro commented 5 years ago

Hi thanks for the reply. I will try for this week.