我有一些Redis代码,它们依赖Proto标准来编组/解组数据。我编写了以下测试,使用戈梅加对proto.Unmarshal
进行测试
b64Decoded, err := base64.StdEncoding.DecodeString("derp")
Expect(err).ShouldNot(HaveOccurred())
var item testRecord
err = proto.Unmarshal(b64Decoded, &item)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(Equal("proto:\u00a0cannot parse invalid wire-format data"))
但是,最后的断言失败了,因为预期的错误字符串是proto: cannot parse invalid wire-format data
。这里最明显的解决办法是改变它,当我改变它时,错误就消失了。直到我修改测试并重新运行它,在这种情况下,测试再次失败,告诉我字符串应该是proto:\u00a0cannot parse invalid wire-format data
。这个循环无止境地在继续。那么,我在这里做错了什么,我该如何解决这个问题?
发布于 2022-05-25 14:20:20
这个问题并不完全像您想象的那样;protobuf
包在输出错误时随机选择一个空格或\u00a0
(我相信它是基于二进制的散列)。你可以看到这个这里
// Deliberately introduce instability into the error message string to
// discourage users from performing error string comparisons.
if detrand.Bool() {
return "proto: " // use non-breaking spaces (U+00a0)
} else {
return "proto: " // use regular spaces (U+0020)
}
因此,您遇到的问题是故意的,目的是防止用户做您正在尝试的事情(依赖于相同的错误)。只有在更改测试时(我猜不是每次更改测试时),Go
才会缓存测试结果(在默认情况下,只有在发生更改时才运行测试)。
关于你能做些什么,我首先建议考虑这个测试是否真的是必要的。包的作者明确表示错误是不稳定的,因此在发布新版本的google.golang.org/protobuf/proto
时,以这种方式进行比较可能会中断。
protobuf
包测试通过调用detrand.Disable()
(例如这里)来解决这个问题。您不能这样做,因为google.golang.org/protobuf/internal/detrand
位于internal
下,因此在不可及下。
如果您真的想解决这个问题,最简单的方法可能是strings.Contains
。
https://stackoverflow.com/questions/72359452
复制相似问题