使用RSpec验证部分参数

Dmy*_*iak 3 ruby unit-testing rspec ruby-on-rails 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
Run Code Online (Sandbox Code Playgroud)

要回答这个问题,请使用注释修复该行.

谢谢.

Dmy*_*iak 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
Run Code Online (Sandbox Code Playgroud)