RSpec:如何通过对象标识比较have_received参数?



我习惯使用expect(subject/double).to haved_received(:a_method).with(args).exactly(n).times来测试一个方法是否被调用了一些特定的参数,并且恰好被调用了{n}次。但是今天它被Comparable对象的参数打破了,看看下面的代码:

设置
class A; end
class B
include Comparable
attr_reader :val
def initialize(val)
@val = val
end
def <=>(other)
self.val <=> other.val
end
end
class S
def call(x); end
end
s = S.new
allow(s).to receive(:call)

现在下面的测试通过了正常对象A

a1 = A.new
a2 = A.new
s.call(a1)
s.call(a2)
expect(s).to have_received(:call).with(a1).exactly(1).times
expect(s).to have_received(:call).with(a2).exactly(1).times

Comparable对象B失败

b1 = B.new(0)
b2 = B.new(0)
s.call(b1)
s.call(b2)
expect(s).to have_received(:call).with(b1).exactly(1).times
expect(s).to have_received(:call).with(b2).exactly(1).times

在调试中看到rspec匹配器调用太空船操作符<=>来验证参数,因此它认为b1和b2是相同的

Failure/Error: expect(s).to have_received(:call).with(b1).exactly(1).times
expected: 1 time with arguments:
received: 2 times with arguments:

我该怎么做才能通过考试?

这是因为Comparable实现了==,所以你的对象在==方面被视为是平等的:

b1 = B.new(0)
b2 = B.new(0)
b1 == b2 #=> true

要基于对象标识设置约束,您可以使用equal匹配器:(或其别名an_object_equal_to/equal_to)

expect(s).to have_received(:call).with(an_object_equal_to(b1)).once

下面,这个匹配器调用equal?:

b1 = B.new(0)
b2 = B.new(0)
b1.equal?(b2) #=> false

我的解决方案:使用have_attributes匹配器来检查对象参数的object_id

expect(s).to have_received(:call).with(have_attributes(object_id: b1.object_id))
.exactly(1).times
expect(s).to have_received(:call).with(have_attributes(object_id: b2.object_id))
.exactly(1).times

相关内容

  • 没有找到相关文章

最新更新