Does an RSpec2 matcher for matching Hashes exist?

前端 未结 3 2175
渐次进展
渐次进展 2021-02-12 23:10

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 tha

相关标签:
3条回答
  • 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

    0 讨论(0)
  • 2021-02-12 23:20
    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.

    0 讨论(0)
  • 2021-02-12 23:38

    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

    0 讨论(0)
提交回复
热议问题