How can I test that a method returns a hash that contains specific keys and the values for those keys are not nil using RSpec?
Typically when an element is passed into a hash with no matching key, the hash returns nil .
A Hash is a dictionary-like collection of unique keys and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type. Hashes enumerate their values in the order that the corresponding keys were inserted.
I'd write:
describe MyObject do
describe "#my_method" do
subject(:my_method) { MyObject.new.my_method }
it { is_expected.to be_a_kind_of(Hash) }
specify { expect(my_method.keys).to include(:key1, :key2) }
specify { expect(my_method.values).not_to include(nil) }
end
end
It may happen that you have to use keys in inverted commas "key1", "key2". Otherwise it may throw error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With