我有一个测试,它比较结构实例(MyStruct
(的两个指针的相等性。指针存储在其他结构(container1
和container2
(中。以下是我如何检查相等性:
require.Equal(*container1.MyStruct, *container2.MyStruct)
CCD_ 5方法来自Testify库:https://github.com/stretchr/testify/blob/master/require/require_forward.go#L128
当我在Mac上运行此测试时,它就通过了。然而,当我将更改推送到构建服务器(Linux(时,测试失败了。错误消息是标准的Error: Not equal...
消息,但当我将输出中显示的预期消息和实际消息进行比较时,它们完全相同!
我想Go可能是在比较指针的地址而不是内容,但上的评论需要。平等让这看起来不太可能:
// Pointer variable equality is determined based on the equality of the referenced values (as opposed to the memory addresses)
知道这里发生了什么吗?Go在Mac和Linux上的表现不同吗?
我不能在这里发布细节,因为这是我在工作中接触到的代码库中看到的一个问题。
问题是Equal
函数的"应为。。。实际"当结构包含Time
对象时,打印该结构的逻辑。以下是错误输出中的一个片段:(我已经删除了结构中不相关的字段(:
Error Trace: my_test.go:388
my_test.go:50
Error: Not equal:
expected: {
BuildStartDateUtc: 2020-08-17 22:43:05.330062876 +0000 UTC,
actual : {
BuildStartDateUtc: 2020-08-17 22:43:05.330062876 +0000 UTC,
}
字段看起来是一样的。然而,如果我直接比较MyStruct.BuildStartDateUtc
,我得到的是:
expected: &time.Time{wall:0x389ccfe6, ext:63733398658, loc:(*time.Location)(0x101d4e0)}
actual : &time.Time{wall:0x389ccfe6, ext:63733398658, loc:(*time.Location)(nil)}
";loc";实际中缺少字段。这就是导致比较失败的原因,但由于";loc";很明显,当我们比较包含Time
的结构时,输出中没有包括它,我们会得到一个非常令人困惑的";应为。。。实际"实际值/预期值看起来完全相等的输出。
当我同步正在比较的Time
的Location
时,测试通过了。
看起来可能与此Testify问题有关:https://github.com/stretchr/testify/issues/984