Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add hook scope aliases `:example` and `:group` #297

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+56 −1
Split
View
@@ -142,7 +142,18 @@ def scope_and_options_from(scope=:each, options={})
options = scope
scope = :each
end
- return scope, options
+ return normalized_scope_for(scope), options
+ end
+
+ def scope_aliases
+ @scope_aliases ||= {
+ :example => :each,
+ :group => :all,
+ }
+ end
+
+ def normalized_scope_for(scope)
+ scope_aliases[scope] || scope
end
end
end
@@ -244,6 +244,50 @@ module RSpec::Core
end
describe "#before, after, and around hooks" do
+ describe "scope aliasing" do
+ it "aliases the `:group` hook scope to `:all`" do
+ group = ExampleGroup.describe
+ order = []
+ group.before(:group) { order << 1 }
+ group.example("example") {}
+
+ group.hooks[:before][:all].run_all(group)
+ order.should == [1]
+ end
+
+ it "aliases the `:example` hook scope to `:each`" do
+ group = ExampleGroup.describe
+ order = []
+ group.before(:example) { order << 1 }
+ group.example("example") {}
+
+ group.hooks[:before][:each].run_all(group)
+ order.should == [1]
+ end
+
+ it "should work with `before`" do
+ group = ExampleGroup.describe
+ order = []
+ group.before(:group) { order << 1 }
+ group.before(:example) { order << 2 }
+ group.example("example") {}
+
+ group.run
+ order.should == [1, 2]
+ end
+
+ it "should work with `after`" do
+ group = ExampleGroup.describe
+ order = []
+ group.after(:example) { order << 1 }
+ group.after(:group) { order << 2 }
+
+ group.example("example") {}
+
+ group.run
+ order.should == [1, 2]
+ end
+ end
it "runs the before alls in order" do
group = ExampleGroup.describe