elliotchance / redismock

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

Pipelines example? #7

Closed mikepc closed 5 years ago

mikepc commented 5 years ago

` func (r PublishRequest) saveToRedis(b string) (ok bool) { q := r.keyConfig.QueueKey c := r.keyConfig.QueueChannel t := r.keyConfig.CacheKey rdb := r.rdb exp := time.Duration(r.cacheDurationSeconds)*time.Second pipe := rdb.TxPipeline()

pipe.LPush(q, b).Err()
pipe.Publish(c, r.id).Err()
pipe.Set(t, b, exp).Err()

if _, err := pipe.Exec(); err != nil {
    log.Errorf("error saving in redis: %s", err.Error())
    ok = false
}

ok = true
return

}

`

Trying to unit test this function has been an adventure.

func (suite *PubSuite) TestSaveToRedis() { js, _ := suite.pr.packageMessage(suite.ob)

r := suite.pr

p := SetupRedis()

suite.rdb.On("TxPipeline").Return(p.TxPipeline())

//exp := time.Duration(r.cacheDurationSeconds) * time.Second
p.On("LPush").Return(redis.NewIntCmd(1))

suite.rdb.On("Publish").Return(redis.NewIntCmd(1))

suite.rdb.On("Set").Return(redis.NewStatusCmd("OK",nil))

suite.rdb.On("Exec").Return(redis.NewStatusCmd("OK", nil))

r.saveToRedis(js)

suite.rdb.AssertExpectations(suite.T())

} `

I've stumbled through several issues, but I think I'm just missing something simple. Do you have any code samples that will test a TxPipeline?

The core issue I'm having is that the mock client returns a redis.TxPipeline instead of a mock Cmdable, and I understand it has to to meet the interface, but how should I go about making assertions on these calls?

Also, since all of the mocks are configured for 0 parameters, I'm finding it extremely troublesome to validate parameters being passed into them. How do I go about validating the parameters as well?

elliotchance commented 5 years ago

Hi Mike, did you close the issue because you found a solution?

mikepc commented 5 years ago

Yes, and I apologize for posting it. Fundamentally I realized all I needed to do is ask the db what the values were, giving me a full integration test. That's why all of the mocks can't test args, it isn't the point of the library.

My java/C# instincts kicked in and I wanted to test the parameters passed in, but I shouldn't I guess. If you have any advice or code samples you feel would be relevant I would love them.

Thank you so much for responding!

On Fri, May 10, 2019 at 5:41 PM Elliot Chance notifications@github.com wrote:

Hi Mike, did you close the issue because you found a solution?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/elliotchance/redismock/issues/7#issuecomment-491464919, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGWREY6KPV4P6U22B2PR4TPUYI4ZANCNFSM4HMDWBQQ .

dylanlive commented 4 years ago

I'm also curious in this. I'm assuming redismock doesn't directly support mocking redis.Pipeliner?

I generated my own mock for now using Mockery mockery -dir %GOPATH%/src/github.com/go-redis/redis -name Pipeliner

And doing something like:

r := newTestRedis()
mockPipe := new(mocks.Pipeliner)
mockPipe.On("HGetAll", mock.Anything).Return(....) // pseudo code... fill in as normal
r.On("TxPipeline").Return(mockPipe)