How to test with Rspec that a key exists inside a hash that contains an array of hashes

RSpec allows you to use the have_key predicate matcher to validate the presence of a key via has_key?, like so:

subject { described_class.order_items_by_revenue }

it "includes revenue key" do
  expect(subject.first).to have_key(:revenue)
end

If you explicitly want to test only the first hash in your array mapped to :data, here are your expects given what you wrote above:

data = subject[:data].first
expect(data).not_to be_nil
expect(data.has_key?(:revenue)).to be_truthy
expect(data[:revenue]).to eq 600

Alternatively, for the second expectation, you could use expect(data).to have_key(:revenue) as Chris Heald pointed out in his answer which has a much nicer failure message as seen in the comments.

  1. The first "expectation" test if the subject has the first hash. (You could alternately test if the array is empty?)
  2. The next expectation is testing if the first hash has the key :revenue
  3. The last expectation tests if the first hash :revenue value is equal to 600

You should read up on RSpec, it's a very powerful and usefull testing framework.


This issue can be solved via testing of each hash by key existing with appropriate type of value. For example:

describe 'GetCatsService' do
  subject { [{ name: 'Felix', age: 25 }, { name: 'Garfield', age: 40 }] }

  it { is_expected.to include(include(name: a_kind_of(String), age: a_kind_of(Integer)))}
end

# GetCatsService
#  should include (include {:name => (a kind of String), :age => (a kind of Integer)})

Tags:

Ruby

Hash

Rspec