我有一个规范,期望一个类接收带有一些参数的方法。我只想检查参数哈希中的键/值对的子集:
it 'calls Stripe::Checkout::Session#create with the correct line items' do
expect(Stripe::Checkout::Session).to receive(:create).with({
line_items: [{
price: "bla",
quantity: 1
}],
}
)
subject
end
这里我只想检查参数哈希中是否存在具有正确值的 line_items。
但是收到的实际哈希值有更多值,我收到以下错误:
#<Stripe::Checkout::Session (class)> received :create with unexpected arguments
expected: ({:line_items=>[{:price=>"bla", :quantity=>1}]}, *(any args))
got: ({:allow_promotion_codes=>true, :automatic_tax=>{:enabled=>true}, :billing_address_collection=>"requir...vh.me/fr/thank-you?checkout_session_id={CHECKOUT_SESSION_ID}", :tax_id_collection=>{:enabled=>true}}, {:stripe_account=>"bla"})
如何告诉 rspec 仅检查键/值对的子集是否存在?
您想要
hash_including
,它匹配哈希的子集:
...to receive(:create).with(hash_including(
line_items: [{
price: "bla",
quantity: 1
}],
)
请参阅文档:https://relishapp.com/rspec/rspec-mocks/v/3-2/docs/setting-constraints/matching-arguments