测试使用redigomock进行事务(MULTI)命令的操作。

huangapple go评论86阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2016年4月1日 01:26:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/36339883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定