我怎么写这个:
给定一个数组b = ['one', 'two', 'three']
我期望值a包含在数组b 。
我想将它与all匹配器一起使用,以便我的最终代码如下所示:
b = ['one', 'two', 'three'] my_list = [ {'type' => 'one'}, {'type' => 'two'}, {'type' => 'three'} ] expect(my_list).to all(include("type" => a_value_included_in(b))正在测试的是:
my_list所有哈希都必须有一个type键,其值在数组b 。
Rspec中有这样的内置匹配器吗?
而且,除了使用明显的反转外,如何检查数组中是否包含值: expect([1, 2, 3]).to include(value) ,在我的示例中,它并不适合?
How can I write this:
Given an array b = ['one', 'two', 'three']
I expect value a to be included in the array b.
I want to use it together with an all matcher so my final code would look like this:
b = ['one', 'two', 'three'] my_list = [ {'type' => 'one'}, {'type' => 'two'}, {'type' => 'three'} ] expect(my_list).to all(include("type" => a_value_included_in(b))Which is testing for:
all hashes from my_list must have a type key whose value is in array b.
Is there such built-in matcher in Rspec?
And how do you check inclusion of a value in the array besides using the obvious reverse: expect([1, 2, 3]).to include(value), which in my example, is not really fitting in?
最满意答案
如果你想检查b 每个字符串是否存在于my_list任何哈希中:
b = ['one', 'two', 'three'] my_list = [ {'type' => 'one'}, {'type' => 'two'}, {'type' => 'three'} ] b.each do |str| expect(my_list.find { |h| h['type'] == str }).to be_present end或者,您可以与:
expect(my_list.map { |h| h['type'] }.sort) to eq(b.sort)expect(my_list.map { |h| b.include?(h['type']) }.uniq).to eq(true)所以当有人添加my_list << { 'type' => 'not_in_list' } ,规范将失败。
If you want to check if every string from b is present in any of the hashes from my_list:
b = ['one', 'two', 'three'] my_list = [ {'type' => 'one'}, {'type' => 'two'}, {'type' => 'three'} ] b.each do |str| expect(my_list.find { |h| h['type'] == str }).to be_present endOr, you can go with:
expect(my_list.map { |h| h['type'] }.sort) to eq(b.sort)expect(my_list.map { |h| b.include?(h['type']) }.uniq).to eq(true)So when someone adds my_list << { 'type' => 'not_in_list' } the spec will fail.
更多推荐
发布评论