This gem adds an RSpec deep_eq
matcher that recursively compares contents of Hash
and Array
data.
Add gem to your Gemfile
:
gem 'rspec_deep_eq'
And run
bundle install
Or install it manually:
gem install rspec_deep_eq
In rails_helper
file, configure deep_eq
matcher for RSpec:
# rails_helper.rb
require 'deep_eq'
RSpec.configure do |config|
config.include DeepEq
end
You can compare everything with deep_eq
matcher, but it's created to compare complex structures like nested hashes and arrays.
describe 'Deep Equal' do
it 'compares' do
data = prepare_data
expect(data).to deep_eq(
id: 1,
items: [
id: 3,
images: [
{
url: 'https://example.com/bass_guitar.png'
},
{
url: 'https://example.com/naked_lady.png'
}
]
],
author: {
name: 'John Doe'
}
)
end
end
Comparing complex data structures can be easily done using built-in RSpec matchers like eq
, match_array
or hash_including
. But when something goes wrong, it displays hard-to-read error output. This matcher is created to make fixing such tests easier.
Read more here.
Published under MIT license.