如何在 rspec 中使用带有嵌套哈希的 expect
How to use expect with nested hashes in rspec
在我的代码中有几个条件如下所示:
if condition1
a = some_hash["a"]["b"]
elsif condition2
b = some_hash["c"]["d"]
如何使用 expect
或一些类似的 rspec 函数测试和模拟这些嵌套哈希分配以确保发生正确的分配?
请参阅以下答案,我相信它可以回答您的问题:
简而言之,您可以使用以下语法:
expect(object).to receive(:method).with(hash_including(some: 'value'))
但是@anothermh 的评论是有效的 — 你没有正确的 RSpec 设置。您需要如下内容:
RSpec.describe ClassName do
it 'should have nested value' do
expect(object).to receive(:method).with(hash_including(some: 'value'))
end
end
在我的代码中有几个条件如下所示:
if condition1
a = some_hash["a"]["b"]
elsif condition2
b = some_hash["c"]["d"]
如何使用 expect
或一些类似的 rspec 函数测试和模拟这些嵌套哈希分配以确保发生正确的分配?
请参阅以下答案,我相信它可以回答您的问题:
简而言之,您可以使用以下语法:
expect(object).to receive(:method).with(hash_including(some: 'value'))
但是@anothermh 的评论是有效的 — 你没有正确的 RSpec 设置。您需要如下内容:
RSpec.describe ClassName do
it 'should have nested value' do
expect(object).to receive(:method).with(hash_including(some: 'value'))
end
end