I'm trying to figure out why a very simple "does this hash have this key" spec I'm writing is failing. Going into my Ruby REPL I am trying the following...
[3] pry(main)> a_hash = {:a=>"A"}
=> {:a=>"A"}
[4] pry(main)> a_hash.should have_key :a
NoMethodError: undefined method `have_key' for main:Object
from (pry):4:in `<main>'
[5] pry(main)> a_hash.keys.length.should == 1
=> true
[8] pry(main)> a_hash.has_key? :a
=> true
The first test is obviously what I want to get working and the second test I'm running just to verify that RSpec is loaded in my REPL environment.
You can actually have RSpec matchers outside "it" blocks. You just need to include RSpec::Matchers.
[ ~/work/mobile_server (master)]$ irb
>> require 'rspec'
true
>> include RSpec::Matchers
Object < BasicObject
>> {a: 1}.should have_key(:a)
true
You need to actually do this inside an RSpec example, I don't think you can write that kind of code anywhere.
describe "" do
it "has a key" do
...
end
end
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