英文:
Test a transaction (MULTI) command with redigomock
问题
在单元测试中,我该如何设置redigomock来测试包含多个命令的MULTI调用?
英文:
In a unittest, how can I setup redigomock to test a MULTI call with several commands included?
答案1
得分: 1
这没有什么真正的技巧。我在redigomock的测试中找到了一个例子,然后发现我有一个拼写错误,导致错误没有返回(可能是一个bug)。作为参考,
https://github.com/rafaeljusto/redigomock/blob/master/redigomock_test.go#L501 (TestDoFlushesQueue)
展示了一个使用MULTI的测试。如果你正在使用go-check,可以这样写:
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)
(如果有人感兴趣,这是修复拼写错误导致可检测错误的PR https://github.com/rafaeljusto/redigomock/pull/21)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论