aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/core/hash/fetch_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/hash/fetch_spec.rb')
-rw-r--r--spec/ruby/core/hash/fetch_spec.rb14
1 files changed, 8 insertions, 6 deletions
diff --git a/spec/ruby/core/hash/fetch_spec.rb b/spec/ruby/core/hash/fetch_spec.rb
index 5e701b1162..24662c2b0e 100644
--- a/spec/ruby/core/hash/fetch_spec.rb
+++ b/spec/ruby/core/hash/fetch_spec.rb
@@ -1,15 +1,17 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
+require File.expand_path('../../../shared/hash/key_error', __FILE__)
describe "Hash#fetch" do
- it "returns the value for key" do
- { a: 1, b: -1 }.fetch(:b).should == -1
+ context "when the key is not found" do
+ it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, Hash.new(a: 5)
+ it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, {}
+ it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, Hash.new { 5 }
+ it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, Hash.new(5)
end
- it "raises a KeyError if key is not found" do
- lambda { {}.fetch(:a) }.should raise_error(KeyError)
- lambda { Hash.new(5).fetch(:a) }.should raise_error(KeyError)
- lambda { Hash.new { 5 }.fetch(:a) }.should raise_error(KeyError)
+ it "returns the value for key" do
+ { a: 1, b: -1 }.fetch(:b).should == -1
end
it "returns default if key is not found when passed a default" do