Rspec : 是否有一个匹配器来匹配数组的数组,而不是测试顺序
Rspec : is there a matcher to match array of arrays, not testing order
我尝试测试两个数组的数组是否包含相同的元素,而不测试元素的顺序。
(Rails 5.2 / Rspec-rails 3.8.2)
示例:
[['a1', 'a2'], ['b1', 'b2']]
[['b2', 'b1'], ['a2', 'a1']]
我尝试使用 match_array 和 contain_exactly 但这只适用于我的数组的第一层。
tab1 = [['a1', 'a2'], ['b1', 'b2']]
tab2 = [['b1', 'b2'], ['a1', 'a2']]
tab3 = [['a2', 'a1'], ['b2', 'b1']]
tab4 = [['b2', 'b1'], ['a2', 'a1']]
expect(tab1).to match_array tab2 # true
expect(tab1).to match_array tab3 # false
expect(tab1).to match_array tab4 # false
有没有匹配器可以做到这一点?或者也许是一种使用可组合匹配器的简单方法?
谢谢
编辑
我找到的解决方案是:
expect(tab1).to contain_exactly(contain_exactly('a1', 'a2'),
contain_exactly('b1', 'b2'))
但我想找到这样的东西
expect(tab1).to ....... tab2
有两种简单的方法可以实现
describe 'two array with random order' do
it 'arrays are equals if content is same' do
tab1 = [['a1', 'a2'], ['b1', 'b2']]
tab2 = [['b1', 'b2'], ['a1', 'a2']]
tab3 = [['a2', 'a1'], ['b2', 'b1']]
tab4 = [['b2', 'b1'], ['a2', 'a1']]
#option 1
expect(tab1.sort).to match_array tab2.sort
expect(tab1.sort).not_to match_array tab3.sort
expect(tab1.map(&:sort).sort).to match_array tab3.map(&:sort).sort
expect(tab1.sort).not_to match_array tab4.sort
# Option 2
expect(tab1).to include *tab2
expect(tab1).not_to include *tab3
expect(tab1.map(&:sort)).to include *tab3.map(&:sort)
expect(tab1).not_to include *tab4
end
end
我想我找到了解决办法。
如果您认为它并不总是有效,请发表评论。
it 'checks that arrays contain the same elements (do not check order)' do
tab1 = [['a1', 'a2'], ['b1', 'b2']]
tab4 = [['b2', 'b1'], ['a2', 'a1']]
expect(tab1.map(&:sort)).to match_array(tab4.map(&:sort))
end
这里的其他答案都不准确。您应该使用 contain_exactly
匹配器。参见 https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/contain-exactly-matcher
我尝试测试两个数组的数组是否包含相同的元素,而不测试元素的顺序。 (Rails 5.2 / Rspec-rails 3.8.2)
示例:
[['a1', 'a2'], ['b1', 'b2']]
[['b2', 'b1'], ['a2', 'a1']]
我尝试使用 match_array 和 contain_exactly 但这只适用于我的数组的第一层。
tab1 = [['a1', 'a2'], ['b1', 'b2']]
tab2 = [['b1', 'b2'], ['a1', 'a2']]
tab3 = [['a2', 'a1'], ['b2', 'b1']]
tab4 = [['b2', 'b1'], ['a2', 'a1']]
expect(tab1).to match_array tab2 # true
expect(tab1).to match_array tab3 # false
expect(tab1).to match_array tab4 # false
有没有匹配器可以做到这一点?或者也许是一种使用可组合匹配器的简单方法? 谢谢
编辑 我找到的解决方案是:
expect(tab1).to contain_exactly(contain_exactly('a1', 'a2'),
contain_exactly('b1', 'b2'))
但我想找到这样的东西
expect(tab1).to ....... tab2
有两种简单的方法可以实现
describe 'two array with random order' do
it 'arrays are equals if content is same' do
tab1 = [['a1', 'a2'], ['b1', 'b2']]
tab2 = [['b1', 'b2'], ['a1', 'a2']]
tab3 = [['a2', 'a1'], ['b2', 'b1']]
tab4 = [['b2', 'b1'], ['a2', 'a1']]
#option 1
expect(tab1.sort).to match_array tab2.sort
expect(tab1.sort).not_to match_array tab3.sort
expect(tab1.map(&:sort).sort).to match_array tab3.map(&:sort).sort
expect(tab1.sort).not_to match_array tab4.sort
# Option 2
expect(tab1).to include *tab2
expect(tab1).not_to include *tab3
expect(tab1.map(&:sort)).to include *tab3.map(&:sort)
expect(tab1).not_to include *tab4
end
end
我想我找到了解决办法。 如果您认为它并不总是有效,请发表评论。
it 'checks that arrays contain the same elements (do not check order)' do
tab1 = [['a1', 'a2'], ['b1', 'b2']]
tab4 = [['b2', 'b1'], ['a2', 'a1']]
expect(tab1.map(&:sort)).to match_array(tab4.map(&:sort))
end
这里的其他答案都不准确。您应该使用 contain_exactly
匹配器。参见 https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/contain-exactly-matcher