将对象作为参数传递给使用Mocha的存根方法

Ale*_*yne 12 ruby mocking mocha.js stubbing

Foo.expects(:bar)
Foo.bar(:abc => 123, :xyz => 987)

# assert Foo.bar was called with a hash that has a key of :abc == 123
Run Code Online (Sandbox Code Playgroud)

基本上我想检查作为参数传递给stubbed方法的对象,以便检查该对象的值.在我的情况下,我无法使用,Foo.expects(:bar).with({:abc => 123})因为我知道对象不会彼此相等.我只想比较参数的子值.

当然这是可能的,我在这里找不到语法或策略.

Ale*_*yne 21

我想到了!原来with可以采取阻止.

Foo.expects(:bar).with do |the_hash|
  the_hash[:abc] == 123
end
Run Code Online (Sandbox Code Playgroud)