我试图断言一个对象数组包含一个预期的对象。然而,即使数组确实包含预期的对象(至少在我看来他们是这样),规范测试仍然失败,并表示数组不包括对象。
下面是一个测试:
bubble_dtos = [...]
expected_bubble = ...
expect(bubble_dtos).to include(expected_bubble)这是测试结果:
Failure/Error: expect(bubble_dtos).to include(expected_bubble)
expected [<Soda::DTO::Bubble container_id=594, bubble_type="co2", flavor_id=3515, bubble_sid="soda::bubble:dbid/1413", id=1413, desc="desc1">, <Soda::DTO::Bubble container_id=594, bubble_type="co2", flavor_id=3519, bubble_sid="soda::bubble:dbid/1414", id=1414, desc="desc2">, <Soda::DTO::Bubble container_id=594, bubble_type="co2", flavor_id=3528, bubble_sid="soda::bubble:dbid/1421", id=1421, desc="desc3">] to include <Soda::DTO::Bubble container_id=594, bubble_type="co2", flavor_id=3528, bubble_sid="soda::bubble:dbid/1421", id=1421, desc="desc3">数组.include?只检查对象引用而不检查对象内容吗?
发布于 2021-09-12 03:11:14
包括?(对象)→真或假 如果给定对象存在于self中(即,如果有任何元素==对象),则返回true,否则返回false。
如果您的DTO没有实现相等操作符==,那么它将使用Object#==,它只比较对象引用。
要将运算符添加到类中,请执行以下操作:
def ==(object)
# handle different type
return false unless object.is_a?(Soda::DTO::Bubble)
# compare internal variables etc.
object.container_id == self.container_id
endhttps://stackoverflow.com/questions/69143459
复制相似问题