duanfeng7756
duanfeng7756
2016-03-31 17:26

使用Redigomock测试事务(MULTI)命令

已采纳

In a unittest, how can I setup redigomock to test a MULTI call with several commands included?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • du3979 du3979 5年前

    There is no real trick to this. I found an example in the redigomock tests & then found I had a typo, which caused an error that was never returned (probably a bug). For reference,

    https://github.com/rafaeljusto/redigomock/blob/master/redigomock_test.go#L501 (TestDoFlushesQueue)

    shows a test that uses MULTI. If you're using go-check, it becomes something like

    connection := redigomock.NewConn()
    cmd1 := connection.Command("MULTI")
    cmd2 := connection.Command("SET", "person-123", 123456)
    cmd3 := connection.Command("EXPIRE", "person-123", 1000)
    cmd4 := connection.Command("EXEC").Expect([]interface{}{"OK", "OK"})
    c.Check(connection.Stats(cmd1), Equals, 1)
    c.Check(connection.Stats(cmd2), Equals, 1)
    c.Check(connection.Stats(cmd3), Equals, 1)
    c.Check(connection.Stats(cmd4), Equals, 1)
    

    (and if anyone is curious, here is the PR so that typos result in detectable errors https://github.com/rafaeljusto/redigomock/pull/21)

    点赞 评论 复制链接分享

为你推荐