Skip to content
This repository
Browse code

add example.exception

- Closes #401.
  • Loading branch information...
commit 1c3f47bccdd35a4d8ff95bc8375a9230aa958c2c 1 parent e2eb136
David Chelimsky authored June 07, 2011
7  Changelog.md
Source Rendered
... ...
@@ -1,3 +1,10 @@
  1
+### dev
  2
+
  3
+[full changelog](http://github.com/rspec/rspec-core/compare/v2.6.4...master)
  4
+
  5
+* Enhancements
  6
+  * Add example.exception (useful for extensions)
  7
+
1 8
 ### 2.6.4 / 2011-06-06
2 9
 
3 10
 [full changelog](http://github.com/rspec/rspec-core/compare/v2.6.3...v2.6.4)
6  lib/rspec/core/example.rb
@@ -4,6 +4,10 @@ class Example
4 4
 
5 5
       attr_reader :metadata, :options, :example_group_instance
6 6
 
  7
+      # Returns the first exception raised, if any, in the context of running
  8
+      # this example.
  9
+      attr_reader :exception
  10
+
7 11
       def self.delegate_to_metadata(*keys)
8 12
         keys.each do |key|
9 13
           define_method(key) {@metadata[key]}
@@ -23,7 +27,7 @@ def example_group
23 27
         @example_group_class
24 28
       end
25 29
 
26  
-      def around_hooks
  30
+      def around_hooks # :nodoc:
27 31
         @around_hooks ||= example_group.around_hooks_for(self)
28 32
       end
29 33
 
16  spec/rspec/core/example_spec.rb
@@ -16,6 +16,21 @@ def metadata_hash(*args)
16 16
     end
17 17
   end
18 18
 
  19
+  describe "#exception" do
  20
+    it "supplies the first exception raised, if any" do
  21
+      example = example_group.example { raise "first" }
  22
+      example_group.after { raise "second" }
  23
+      example_group.run
  24
+      example.exception.message.should eq("first")
  25
+    end
  26
+
  27
+    it "returns nil if there is no exception" do
  28
+      example = example_group.example('example') { }
  29
+      example_group.run
  30
+      example.exception.should be_nil
  31
+    end
  32
+  end
  33
+
19 34
   describe "auto-generated example descriptions" do
20 35
     let(:generated_description) { "the generated description" }
21 36
     let(:rspec_example) { example_group.specify { 5.should == 5 } }
@@ -269,4 +284,5 @@ def assert(val)
269 284
     end
270 285
 
271 286
   end
  287
+
272 288
 end

0 notes on commit 1c3f47b

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