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](ExampleGroup). An instance of ‘Example` is returned by the [example](ExampleGroup#example-instance_method) method available in examples, [before](Hooks#before-instance_method) and [after](Hooks#after-instance_method) hooks, and yielded to [around](Hooks#around-instance_method) hooks.
Defined Under Namespace
Modules: NotPendingExampleFixed, Procsy
Instance Attribute Summary collapse
-
#example_group_instance ⇒ Object
readonly
Returns the example_group_instance that provides the context for running this example.
-
#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.
Class Method Summary collapse
-
.delegate_to_metadata(*keys) ⇒ Object
Used to define methods that delegate to this example’s metadata.
-
.procsy(metadata, &proc) ⇒ Object
Wraps the example block in a Proc so it can invoked using ‘run` or `call` in [around](../Hooks#around-instance_method) hooks.
Instance Method Summary collapse
- #all_apply?(filters) ⇒ Boolean
- #any_apply?(filters) ⇒ Boolean
- #around_hooks ⇒ Object
-
#example_group ⇒ Object
Returns the example group class that provides the context for running this example.
-
#fail_with_exception(reporter, exception) ⇒ Object
Used internally to set an exception and fail without actually executing the example when an exception is raised in before(:all).
-
#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.
-
#set_exception(exception) ⇒ Object
Used internally to set an exception in an after hook, which captures the exception but doesn’t raise it.
Constructor Details
#initialize(example_group_class, description, metadata, example_block = nil) ⇒ Example
Creates a new instance of Example.
45 46 47 48 49 50 |
# File 'lib/rspec/core/example.rb', line 45 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
#example_group_instance ⇒ Object (readonly)
Returns the example_group_instance that provides the context for running this example.
38 39 40 |
# File 'lib/rspec/core/example.rb', line 38 def example_group_instance @example_group_instance end |
#exception ⇒ Object (readonly)
Returns the first exception raised in the context of running this example (nil if no exception is raised)
26 27 28 |
# File 'lib/rspec/core/example.rb', line 26 def exception @exception end |
#metadata ⇒ Object (readonly)
Returns the metadata object associated with this example.
31 32 33 |
# File 'lib/rspec/core/example.rb', line 31 def @metadata end |
Class Method Details
.delegate_to_metadata(*keys) ⇒ Object
Used to define methods that delegate to this example’s metadata
14 15 16 17 18 |
# File 'lib/rspec/core/example.rb', line 14 def self.(*keys) keys.each do |key| define_method(key) {@metadata[key]} end end |
.procsy(metadata, &proc) ⇒ Object
Wraps the example block in a Proc so it can invoked using ‘run` or `call` in [around](../Hooks#around-instance_method) hooks.
112 113 114 |
# File 'lib/rspec/core/example.rb', line 112 def self.procsy(, &proc) Proc.new(&proc).extend(Procsy).with() end |
Instance Method Details
#all_apply?(filters) ⇒ Boolean
142 143 144 |
# File 'lib/rspec/core/example.rb', line 142 def all_apply?(filters) @metadata.all_apply?(filters) || @example_group_class.all_apply?(filters) end |
#any_apply?(filters) ⇒ Boolean
137 138 139 |
# File 'lib/rspec/core/example.rb', line 137 def any_apply?(filters) .any_apply?(filters) end |
#around_hooks ⇒ Object
147 148 149 |
# File 'lib/rspec/core/example.rb', line 147 def around_hooks @around_hooks ||= example_group.around_hooks_for(self) end |
#example_group ⇒ Object
Returns the example group class that provides the context for running this example.
59 60 61 |
# File 'lib/rspec/core/example.rb', line 59 def example_group @example_group_class end |
#fail_with_exception(reporter, exception) ⇒ Object
Used internally to set an exception and fail without actually executing the example when an exception is raised in before(:all).
163 164 165 166 167 |
# File 'lib/rspec/core/example.rb', line 163 def fail_with_exception(reporter, exception) start(reporter) set_exception(exception) finish(reporter) end |
#options ⇒ Object
access options via metadata instead
53 54 55 |
# File 'lib/rspec/core/example.rb', line 53 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
69 70 71 72 73 74 75 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 |
# File 'lib/rspec/core/example.rb', line 69 def run(example_group_instance, reporter) @example_group_instance = example_group_instance @example_group_instance.example = self start(reporter) begin unless pending with_around_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 |
#set_exception(exception) ⇒ Object
Used internally to set an exception in an after hook, which captures the exception but doesn’t raise it.
155 156 157 |
# File 'lib/rspec/core/example.rb', line 155 def set_exception(exception) @exception ||= exception end |