我想使用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/

10-14 17:51