I'm writing tests in rspec and am trying to declare some variables with let
.
describe 'my_test' do
let(:params) {{
:happy => 1,
:sad => 0,
}}
context 'mixed' do
let(:params) {{
:happy => 1,
:sad => 1,
}}
end
end
But then I saw how variables can be ovewritten with super
, which would be convenient for long lists of params: http://myronmars.to/n/dev-blog/2013/02/rspec-2-13-is-released
So my question is, how do I overwrite just a single value in the original hash? I've tried searching but can only find ways to overwrite all values. Does anything like below exist?
let(:hash) { super().updatehash('sad', '1') }
You can overload hash variables using Hash#merge
let(:hash) { super().merge(:sad => '1') }
it will overwrite any key-values in the original hash with the key-value pairs you provide in the hash to merge over it.
If you'd like a link to the doco for merge, apidock is good:
http://apidock.com/ruby/Hash/merge
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