如何期望RSpec的一些(但不是全部)参数应该存在?

时间:2013-10-07 19:32:31

标签: ruby rspec expectations

class Foo
  def bar(a, b)
  ...

Foo.should_receive( :bar )

期望使用任何参数调用bar。

Foo.should_receive( :bar ).with( :baz, :qux )

期望:baz和:qux作为params传入。

如何期待第一个参数相等:baz,而不关心其他参数?

3 个答案:

答案 0 :(得分:40)

使用anything匹配器:

Foo.should_receive(:bar).with(:baz, anything)

答案 1 :(得分:9)

还有另一种方法,这是receive的块形式:https://relishapp.com/rspec/rspec-mocks/v/3-2/docs/configuring-responses/block-implementation#use-a-block-to-verify-arguments

expect(Foo).to receive(:bar) do |args|
  expect(args[0]).to eq(:baz)
end

答案 2 :(得分:7)

对于 Rspec 1.3 anything,当您的方法收到哈希作为参数时,该功能无效,请尝试使用{{1} }:

hash_including(:key => val)
相关问题