我想使用 rspec 提供的参数匹配器来匹配哈希数组。这是我想要的理想代码:
context 'logging stock levels' do
subject { double(:stock_logger, stock_updated: nil) }
let(:stock_importer) { described_class.new(logger: subject) }
before(:each) { stock_importer.import }
it { is_expected.to have_received(:stock_updated)
.with(array_including(hash_including('sku', 'count_on_hand'))) }
end
这对我来说是参数不匹配的错误。我能想到的唯一可行的解决方案如下:
context 'logging stock levels' do
subject { double(:stock_logger, stock_updated: nil) }
let(:stock_importer) { described_class.new(logger: subject) }
before(:each) { stock_importer.import }
it do
is_expected.to have_received(:stock_updated) do |stock_levels|
expect(stock_levels).to include(include('sku', 'count_on_hand'))
end
end
end
我是不是做错了什么?
最佳答案
对于 Rspec 3.3,这对我有用:
expect(<Object>).to receive(:method).with(
array_including(hash_including( {"id" => some_id} ))
)
关于ruby - RSpec 参数匹配哈希数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26612397/