我想知道是否有辦法用RSpec實作我想要的東西。所以我想測驗一個類似于這樣的哈希值陣列:
我想知道是否有辦法用RSpec來實作我的愿望。
array_example =
[
{
id: 1,
link: "www.some-link.com/path_im_not_interested_in"。
},
{
id: 2,
link: "www.example-link.com/another_useless_path".
}
]
我想做集成測驗,其中我不會在鏈接中包含這些路徑。我只對檢查www.some-link.com和www.example-link.com是否存在于陣列中的這些哈希值感興趣。像這樣:
expect(array_example).to match_array(
[
{
id: 1,
link: "www.some-link.com"。
},
{
id: 2,
link: "www.example-link.com".
}
]
但這個顯然是不行的。有什么方法可以實作我想要的嗎?我想避免使用自定義匹配器。
uj5u.com熱心網友回復:
你可以使用組成匹配器:
expect(array_example)
.to contain_exactly(
a_hash_including(id: 1, link: a_string_including("www.some-link.com") )。)
a_hash_including(id: 2, link: a_string_including("www.example-link.com") )
)
參見https://rspec.info/blog/2014/01/new-in-rspec-3-composable-matchers/以了解更多資訊。
a_hash_including的引數不需要覆寫整個哈希,它將只檢查你所傳遞的鍵。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/323822.html
標籤:
上一篇:在同一行接收數值
