Class: RSpec::Core::Example
- Inherits:
-
Object
- Object
- RSpec::Core::Example
- Defined in:
- lib/rspec/core/example.rb
Overview
Wrapper for an instance of a subclass of ExampleGroup. An
instance of Example
is returned by the
example method available in
examples, before and
after hooks, and yielded to
around hooks.
Instance Attribute Summary collapse
-
#exception ⇒ Object
readonly
Returns the first exception raised in the context of running this example (nil if no exception is raised).
-
#metadata ⇒ Object
readonly
Returns the metadata object associated with this example.
Instance Method Summary collapse
-
#description ⇒ Object
Returns the string submitted to
example
or its aliases (e.g.specify
,it
, etc). -
#example_group ⇒ Object
Returns the example group class that provides the context for running this example.
-
#initialize(example_group_class, description, metadata, example_block = nil) ⇒ Example
constructor
Creates a new instance of Example.
-
#options ⇒ Object
deprecated
Deprecated.
access options via metadata instead
-
#run(example_group_instance, reporter) ⇒ Object
private
instance_evals the block submitted to the constructor in the context of the instance of ExampleGroup.
Constructor Details
#initialize(example_group_class, description, metadata, example_block = nil) ⇒ Example
Creates a new instance of Example.
52 53 54 55 56 57 |
# File 'lib/rspec/core/example.rb', line 52 def initialize(example_group_class, description, , example_block=nil) @example_group_class, @options, @example_block = example_group_class, , example_block @metadata = @example_group_class..for_example(description, ) @exception = nil @pending_declared_in_example = false end |
Instance Attribute Details
#exception ⇒ Object (readonly)
Returns the first exception raised in the context of running this example (nil if no exception is raised)
33 34 35 |
# File 'lib/rspec/core/example.rb', line 33 def exception @exception end |
#metadata ⇒ Object (readonly)
Returns the metadata object associated with this example.
38 39 40 |
# File 'lib/rspec/core/example.rb', line 38 def @metadata end |
Instance Method Details
#description ⇒ Object
Returns the string submitted to example
or its aliases (e.g.
specify
, it
, etc). If no string is submitted (e.g. it { should
do_something }
) it returns the message generated by the matcher if
there is one, otherwise returns a message including the location of the
example.
25 26 27 |
# File 'lib/rspec/core/example.rb', line 25 def description [:description].to_s.empty? ? "example at #{location}" : [:description] end |
#example_group ⇒ Object
Returns the example group class that provides the context for running this example.
66 67 68 |
# File 'lib/rspec/core/example.rb', line 66 def example_group @example_group_class end |
#options ⇒ Object
access options via metadata instead
60 61 62 |
# File 'lib/rspec/core/example.rb', line 60 def @options end |
#run(example_group_instance, reporter) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
instance_evals the block submitted to the constructor in the context of the instance of ExampleGroup
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rspec/core/example.rb', line 76 def run(example_group_instance, reporter) @example_group_instance = example_group_instance @example_group_instance.example = self start(reporter) begin unless pending with_around_each_hooks do begin run_before_each @example_group_instance.instance_eval(&@example_block) rescue Pending::PendingDeclaredInExample => e @pending_declared_in_example = e. rescue Exception => e set_exception(e) ensure run_after_each end end end rescue Exception => e set_exception(e) ensure @example_group_instance.instance_variables.each do |ivar| @example_group_instance.instance_variable_set(ivar, nil) end @example_group_instance = nil begin assign_auto_description rescue Exception => e set_exception(e) end end finish(reporter) end |