Wrapper for an instance of a subclass of [ExampleGroup](ExampleGroup). An instance of `Example` is returned by the [example](RSpec::Core::ExampleGroup#example) method available in examples, [before](RSpec::Core::Hooks#before) and [after](RSpec::Core::Hooks#after) hooks, and yielded to [around](RSpec::Core::Hooks#around) hooks. @see ExampleGroup
@attr_reader @private
Returns the #example_group_instance that provides the context for running this example.
@attr_reader
Returns the first exception raised in the context of running this example (nil if no exception is raised)
@attr_reader
Returns the metadata object associated with this example.
@private
Used to define methods that delegate to this example's metadata
# File lib/rspec/core/example.rb, line 14 def self.delegate_to_metadata(*keys) keys.each do |key| define_method(key) {@metadata[key]} end end
Creates a new instance of Example. @param example_group_class the subclass of ExampleGroup in which this Example is declared @param description the String passed to the `it` method (or alias) @param metadata additional args passed to `it` to be used as metadata @param example_block the block of code that represents the example
# File lib/rspec/core/example.rb, line 45 def initialize(example_group_class, description, metadata, example_block=nil) @example_group_class, @options, @example_block = example_group_class, metadata, example_block @metadata = @example_group_class.metadata.for_example(description, metadata) @exception = nil @pending_declared_in_example = false end
@private
Wraps the example block in a Proc so it can invoked using `run` or `call` in [around](../RSpec::Core::Hooks#around) hooks.
# File lib/rspec/core/example.rb, line 112 def self.procsy(metadata, &proc) Proc.new(&proc).extend(Procsy).with(metadata) end
@private
# File lib/rspec/core/example.rb, line 142 def all_apply?(filters) @metadata.all_apply?(filters) || @example_group_class.all_apply?(filters) end
@private
# File lib/rspec/core/example.rb, line 137 def any_apply?(filters) metadata.any_apply?(filters) end
@private
# File lib/rspec/core/example.rb, line 147 def around_hooks @around_hooks ||= example_group.around_hooks_for(self) end
Returns the example group class that provides the context for running this example.
# File lib/rspec/core/example.rb, line 59 def example_group @example_group_class end
@private
Used internally to set an exception and fail without actually executing the example when an exception is raised in before(:all).
# File lib/rspec/core/example.rb, line 163 def fail_with_exception(reporter, exception) start(reporter) set_exception(exception) finish(reporter) end
@deprecated access options via metadata instead
# File lib/rspec/core/example.rb, line 53 def options @options end
@api private @param #example_group_instance the instance of an ExampleGroup subclass instance_evals the block submitted to the constructor in the context of the instance of ExampleGroup
# 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.message 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
@private
Used internally to set an exception in an after hook, which captures the exception but doesn't raise it.
# File lib/rspec/core/example.rb, line 155 def set_exception(exception) @exception ||= exception end