用于匹配Hashes的RSpec2匹配器是否存在? [英] Does an RSpec2 matcher for matching Hashes exist?

查看:106
本文介绍了用于匹配Hashes的RSpec2匹配器是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意未来的读者:RSpec认为你的Hashes不等于?一个可能是一个OrderedHash,但是从常规的RSpec输出你不能说。这是引发这篇文章的问题。



原始问题:

假设我有我想要的规格测试一个方法是否生成适当的Hash。

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' :foo => 1,'baz'=> 2}
subject.some_method_that_should_generate_the_hash.should == expected b $ b end

失败,因为具有相同键值对的不同哈希可能以不同的顺序返回它们的对。结果如下所示:

 失败/错误:subject.some_method_that_should_generate_the_hash.should ==期望
期望值:{:foo = > 1,'baz'=> 2},
got:{'baz'=> 2,:foo => 1}

对于数组,这可以使用=〜运算符来解决。但是,这对于Hashes无效。现在,我使用了

 它应该正确生成哈希值
expected = {:foo => 1,'baz'=> 2}
subject.some_method_that_should_generate_the_hash.each {| k,v |
v.should == expected [k]
}
end



<但似乎不必要的冗长。我希望有一个明显的解决方案。我是否忽略了文档中的某些内容,或者RSpec没有适当的Matcher来实现无序散列平等?

解决方案

描述'Hash'do
let(:x){{:a => 1,:b => 2}}
let(:y){{:b => 2,:a => 1}}

它应该等于==do
x.should == y
end
end

传球。我不确定你的具体情况是怎么回事。你可以分享一些失败的例子吗?



编程Ruby有这样一段话:


Equality - 如果
具有相同的默认值,则两个哈希值相等,它们的
包含相同数量的键,并且
是与
中的每个键相对应的值第一个散列值等于(使用==)至
,即
秒中同一个键的值。



Note to future readers: think RSpec does not consider your Hashes equal? One might be an OrderedHash, but from the regular RSpec output you can't tell. This was the problem that prompted this post.

Original question:

Suppose I have a spec where I want to test that a method generates the appropriate Hash.

it 'should generate the Hash correctly' do
  expected = {:foo => 1, 'baz' => 2}
  subject.some_method_that_should_generate_the_hash.should == expected
end

This often fails, because different Hashes with the same key-value pairs may return their pairs in a different ordered. Results look like:

Failure/Error: subject.some_method_that_should_generate_the_hash.should == expected
expected: {:foo => 1, 'baz' => 2},
     got: {'baz' => 2, :foo => 1}

For arrays, this is solved using the =~ operator. However, that does not work for Hashes. For now, I've resorted to

it 'should generate the Hash correctly' do
  expected = {:foo => 1, 'baz' => 2}
  subject.some_method_that_should_generate_the_hash.each {|k,v|
    v.should == expected[k]
  }
end

but that seems unnecessarily verbose. I expect there to be an obvious solution for this. Am I overlooking something in the docs or doesn't RSpec have a proper Matcher for orderless Hash equality?

解决方案

describe 'Hash' do
  let(:x) { { :a => 1, :b => 2 } }
  let(:y) { { :b => 2, :a => 1 } }

  it "should be equal with ==" do
    x.should == y
  end
end

Passes. I'm not sure what's going on in your specific case. Do you have some failing examples you can share?

Programming Ruby has this to say:

Equality — Two hashes are equal if they have the same default value, they contain the same number of keys, and the value corresponding to each key in the first hash is equal (using ==) to the value for the same key in the second.

这篇关于用于匹配Hashes的RSpec2匹配器是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆