Does an RSpec2 matcher for matching Hashes exist?

假装没事ソ 提交于 2019-12-03 10:27:21
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.

Since 8 months the gem rspec-matchers has support for matching hashes:

expected.should be_hash_matching(subhash_or_equal)

see here for more details: https://github.com/rspec/rspec-expectations/pull/79

I believe the eql? method checks only that the two hashes have the same content So IIRC in Rspec2 you can do:

    expected = {:foo => 1, 'baz' => 2}
    expected.should be_eql({'baz' => 2, :foo => 1})

And the test should pass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!