RSpec 2.7 和哈希应该有_key
我试图弄清楚为什么我正在编写的一个非常简单的“这个哈希是否有这个密钥”规范失败了。进入我的 Ruby REPL,我正在尝试以下操作...
[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
第一个测试显然是我想要进行的工作,而我运行的第二个测试只是为了验证 RSpec 是否已加载到我的 REPL 环境中。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您可以在“it”块之外拥有 RSpec 匹配器。您只需要包含 RSpec::Matchers 即可。
You can actually have RSpec matchers outside "it" blocks. You just need to include RSpec::Matchers.
您需要在 RSpec 示例中实际执行此操作,我认为您不能在任何地方编写此类代码。
You need to actually do this inside an RSpec example, I don't think you can write that kind of code anywhere.