Permalink
Browse files

Force users to explicitly specify hook scope when using symbols as me…

…tadata for hooks.
  • Loading branch information...
1 parent 9cee5e2 commit d8cb6287deff16b0d31b3ea29bf3c246f1fe3866 @myronmarston myronmarston committed Mar 10, 2011
Showing with 33 additions and 2 deletions.
  1. +2 −0 lib/rspec/core/hooks.rb
  2. +31 −2 spec/rspec/core/hooks_spec.rb
View
@@ -145,6 +145,8 @@ def find_hook(hook, scope, example_group_class, example = nil)
def scope_and_options_from(*args)
scope = if [:each, :all, :suite].include?(args.first)
args.shift
+ elsif args.any? { |a| a.is_a?(Symbol) }
+ raise ArgumentError.new("You must explicitly give a scope (:each, :all, or :suite) when using symbols as metadata for a hook.")
else
:each
end
@@ -7,10 +7,10 @@ class HooksHost
end
[:before, :after, :around].each do |type|
- [nil, :each, :all].each do |scope|
+ [:each, :all].each do |scope|
next if type == :around && scope == :all
- describe "##{type}(#{scope ? scope.inspect : 'default scope' })" do
+ describe "##{type}(#{scope})" do
it_behaves_like "metadata hash builder" do
define_method :metadata_hash do |*args|
instance = HooksHost.new
@@ -21,6 +21,35 @@ class HooksHost
end
end
end
+
+ [true, false].each do |config_value|
+ context "when RSpec.configuration.treat_symbols_as_metadata_keys_with_true_values is set to #{config_value}" do
+ before(:each) do
+ Kernel.stub(:warn)
+ RSpec.configure { |c| c.treat_symbols_as_metadata_keys_with_true_values = config_value }
+ end
+
+ describe "##{type}(no scope)" do
+ let(:instance) { HooksHost.new }
+
+ it "defaults to :each scope if no arguments are given" do
+ hooks = instance.send(type) { }
+ hook = hooks.first
+ instance.hooks[type][:each].should include(hook)
+ end
+
+ it "defaults to :each scope if the only argument is a metadata hash" do
+ hooks = instance.send(type, :foo => :bar) { }
+ hook = hooks.first
+ instance.hooks[type][:each].should include(hook)
+ end
+
+ it "raises an error if only metadata symbols are given as arguments" do
+ expect { instance.send(type, :foo, :bar) { } }.to raise_error(ArgumentError)
+ end
+ end
+ end
+ end
end
[:before, :after].each do |type|

0 comments on commit d8cb628

Please sign in to comment.