Skip to content
Browse files

Add specs to show after(:each) is run

- also restructured ExampleGroup.create such that it is defined
  in spec_helper (added to ExampleGroup) and pops itself off the
  global list of example groups.
  • Loading branch information...
1 parent 755ffe0 commit 836370743dad4b4b1cdecd8757af32ed1152137a @dchelimsky dchelimsky committed
Showing with 53 additions and 14 deletions.
  1. +10 −12 lib/rspec/core/example_group.rb
  2. +35 −2 spec/rspec/core/example_spec.rb
  3. +8 −0 spec/spec_helper.rb
View
22 lib/rspec/core/example_group.rb
@@ -103,23 +103,21 @@ def self.describe(*args, &example_group_block)
raise(ArgumentError, "You must supply a block when calling describe") if example_group_block.nil?
@_subclass_count ||= 0
@_subclass_count += 1
+ args << {} unless args.last.is_a?(Hash)
+ args.last.update(:example_group_block => example_group_block)
+ args.last.update(:caller => caller)
+ args.unshift Rspec.configuration unless args.first.is_a?(Rspec::Core::Configuration)
const_set(
"Nested_#{@_subclass_count}",
- _build(Class.new(self), caller, args, &example_group_block)
+ subclass(self, args, &example_group_block)
)
end
- def self.create(*args, &example_group_block)
- _build(dup, caller, args, &example_group_block)
- end
-
- def self._build(klass, given_caller, args, &example_group_block)
- args << {} unless args.last.is_a?(Hash)
- args.last.update(:example_group_block => example_group_block, :caller => given_caller)
- args.unshift Rspec.configuration unless args.first.is_a?(Rspec::Core::Configuration)
- klass.set_it_up(*args)
- klass.module_eval(&example_group_block) if example_group_block
- klass
+ def self.subclass(parent, args, &example_group_block)
+ subclass = Class.new(parent)
+ subclass.set_it_up(*args)
+ subclass.module_eval(&example_group_block) if example_group_block
+ subclass
end
class << self
View
37 spec/rspec/core/example_spec.rb
@@ -62,8 +62,41 @@
end
describe "#run" do
- it "should run after(:each) when the example fails"
- it "should run after(:each) when the example raises an Exception"
+ let(:reporter) { double('reporter').as_null_object }
+
+ it "should run after(:each) when the example passes" do
+ after_run = false
+ group = Rspec::Core::ExampleGroup.create do
+ after(:each) { after_run = true }
+ example('example') { 1.should == 1 }
+ end
+ group.stub(:examples_to_run) { group.examples }
+ group.run(reporter)
+ after_run.should be_true, "expected after(:each) to be run"
+ end
+
+ it "should run after(:each) when the example fails" do
+ after_run = false
+ group = Rspec::Core::ExampleGroup.create do
+ after(:each) { after_run = true }
+ example('example') { 1.should == 2 }
+ end
+ group.stub(:examples_to_run) { group.examples }
+ group.run(reporter)
+ after_run.should be_true, "expected after(:each) to be run"
+ end
+
+ it "should run after(:each) when the example raises an Exception" do
+ after_run = false
+ group = Rspec::Core::ExampleGroup.create do
+ after(:each) { after_run = true }
+ example('example') { raise "this error" }
+ end
+ group.stub(:examples_to_run) { group.examples }
+ group.run(reporter)
+ after_run.should be_true, "expected after(:each) to be run"
+ end
+
end
end
View
8 spec/spec_helper.rb
@@ -30,6 +30,14 @@ def fail_with(message)
end
end
+class Rspec::Core::ExampleGroup
+ def self.create(*args, &example_group_block)
+ args.unshift('example group') if args.empty?
+ describe(*args, &example_group_block || lambda {})
+ Rspec::Core.world.example_groups.pop
+ end
+end
+
def use_formatter(new_formatter)
original_formatter = Rspec.configuration.formatter
Rspec.configuration.instance_variable_set(:@formatter, new_formatter)

0 comments on commit 8363707

Please sign in to comment.
Something went wrong with that request. Please try again.