rspec stub to allow [hash_key] to be passed
you can do this:
allow(menu.menu_items).to receive(:[]).and_return({Beer: 2.0})
You can also pass an specific item if you need:
allow(menu.menu_items).to receive(:[]).with(1).and_return({Beer: 2.0})
The line menu.menu_items[item]
is in reality composed by 3 method calls. []
is a call to the method []
on the Hash
returned by menu_items
.
I assume menu.menu_items
returns a Hash
and not an Array
, given in the spec item
is a Symbol
.
That means your stub requires a little bit more work.
allow(menu).to receive(:menu_items).and_return({ Beer: 2.0 })
Also note, the error
undefined local variable or method `item'
is because you were using item
in the spec, but item
is not defined outside your method.