在一个测试中,我有一个元素的列表actual,它的结构类似于:
type MyStruct struct {
Field1 string
Field2 int32
.
. // a long list of other fields
.
}我想断言actual包含一系列预期元素的元素,但仅考虑Field1和Field2,其他字段与测试无关。我想使用ContainElements匹配器和一些“神奇的”自定义匹配器,如下面的伪代码所示:
expected := []MyStruct{{Field1: "value1", Field2: 1} ...{Field1: "value2", Field2: 2}}
Expect(actual).To(ContainElements(expected), <custom matcher>)我一直在研究1中的WithTransform匹配器,但是我不知道如何在这个上下文中使用它。
1
发布于 2021-02-27 00:35:59
您可以使用MatchFields(IgnoreExtras,Fields{})仅比较给定的字段,因此在您的示例中,它应该如下所示:
ContainElement(MatchFields(IgnoreExtras,Fields{
"Field1": Equal("Value1"),
"Field2": Equal(int32(42)),
}))发布于 2021-07-09 16:47:43
我认为你可以直接使用定制的gomega matcher respect。看起来像是
expected := []MyStruct{{Field1: "value1", Field2: 1} ...{Field1: "value2", Field2: 2}}
Expect(actual).To(Respcet(expected))https://stackoverflow.com/questions/61488725
复制相似问题