"1", "bar"=>"1"}, {"foo"=>"2", "bar"=>"2"}] 使用 Rspec,我想测试 "foo"=> -6ren">
gpt4 book ai didi

ruby - Rspec 匹配哈希数组

转载 作者:数据小太阳 更新时间:2023-10-29 06:32:45 25 4
gpt4 key购买 nike

我有一个哈希数组,为了便于论证,它看起来像这样:

[{"foo"=>"1", "bar"=>"1"}, {"foo"=>"2", "bar"=>"2"}]

使用 Rspec,我想测试 "foo"=> "2" 是否存在于数组中,但我不关心它是第一项还是第二项。我试过:

[{"foo" => "1", "bar" => "2"}, {"foo" => "2", "bar" => "2"}].should include("foo" => "2"))

但这行不通,因为哈希值应该完全匹配。有什么方法可以部分测试每个散列的内容吗?

最佳答案

怎么样?

hashes = [{"foo" => "1", "bar" => "2"}, {"foo" => "2", "bar" => "2"}]
expect(hashes).to include(include('foo' => '2'))

关于ruby - Rspec 匹配哈希数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23815944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com