使用RSpec验证部分参数

时间:2011-12-05 23:05:58

标签: ruby-on-rails ruby unit-testing rspec mocking

我需要在一个参数上设定期望值。如何从RSpec访问收到的参数?

这是我想要实现的目标。

let(:api) { double('API') }

it "should pass :filter in options" do
  api.should_receive(:traverse)
  subject.execute
  args = api.recevied_arguments_for(:traverse) # How to obtain all the arguments?
  args[0].should have_key(:filter)
end

要回答这个问题,请修改注释行。

感谢。

1 个答案:

答案 0 :(得分:10)

刚想通了。 比我想象的要好得多。

let(:api) { double('API') }
it "should pass :filter in options" do
  api.should_receive(:traverse).with hash_including(:filter)
  # or with the exact value
  #api.should_receive(:traverse).with hash_including(:filter => 'something')
  subject.execute
end