class RSpec::Core::Example
of an ‘ExampleGroup`, not in the context of an instance of `Example`.
@note Example blocks are evaluated in the context of an instance
@see ExampleGroup
end
end
auditable.should do_something
log “#{example.full_description}: #{auditable.inspect}”
it “does something” do
shared_examples “auditable” do
end
end
example.run
log example.description
config.around do |example|
end
log example.description
config.after do |example|
end
log example.description
config.before do |example|
RSpec.configure do |config|
@example
on the state of an example’s metadata.
Useful for configuring logging and/or taking some action based
that users may inadvertantly redefine.
example without adding tons of methods directly to the ExampleGroup
This allows us to provide rich metadata about each individual
{MemoizedHelpers::ClassMethods#subject subject} blocks.
{MemoizedHelpers::ClassMethods#let let} and
{Hooks#before before}, {Hooks#after after}, {Hooks#around around},
such as {ExampleGroup.it it} and is yielded to the {ExampleGroup.it it},
‘RSpec::Core::Example` is returned by example definition methods
Wrapper for an instance of a subclass of {ExampleGroup}. An instance of
def self.delegate_to_metadata(key)
- Private: -
def self.delegate_to_metadata(key) define_method(key) { @metadata[key] } end
def all_apply?(filters)
- Private: -
def all_apply?(filters) MetadataFilter.all_apply?(filters, metadata) || @example_group_class.all_apply?(filters) end
def any_apply?(filters)
- Private: -
def any_apply?(filters) MetadataFilter.any_apply?(filters, metadata) end
def around_example_hooks
- Private: -
def around_example_hooks @around_example_hooks ||= example_group.hooks.around_example_hooks_for(self) end
def assign_generated_description
def assign_generated_description if metadata[:description].empty? && (description = RSpec::Matchers.generated_description) metadata[:description] = description metadata[:full_description] << description end rescue Exception => e set_exception(e, "while assigning the example description") ensure RSpec::Matchers.clear_generated_description end
def description
there is one, otherwise returns a message including the location of the
do_something }`) it returns the message generated by the matcher if
`specify`, `it`, etc). If no string is submitted (e.g. `it { is_expected.to
Returns the string submitted to `example` or its aliases (e.g.
def description description = metadata[:description].to_s.empty? ? "example at #{location}" : metadata[:description] RSpec.configuration.format_docstrings_block.call(description) end
def example_group
Returns the example group class that provides the context for running
def example_group @example_group_class end
def fail_with_exception(reporter, exception)
- Private: -
def fail_with_exception(reporter, exception) start(reporter) set_exception(exception) finish(reporter) end
def finish(reporter)
def finish(reporter) pending_message = execution_result.pending_message if @exception record_finished :failed execution_result.exception = @exception reporter.example_failed self false elsif pending_message record_finished :pending execution_result.pending_message = pending_message reporter.example_pending self true else record_finished :passed reporter.example_passed self true end end
def initialize(example_group_class, description, user_metadata, example_block=nil)
- Api: - private
Parameters:
-
example_block
(Proc
) -- the block of code that represents the example -
user_metadata
(Hash
) -- additional args passed to `it` to be used as metadata -
description
(String
) -- the String passed to the `it` method (or alias) -
example_group_class
(Class
) -- the subclass of ExampleGroup in which this Example is declared
def initialize(example_group_class, description, user_metadata, example_block=nil) @example_group_class = example_group_class @example_block = example_block @metadata = Metadata::ExampleHash.create( @example_group_class.metadata, user_metadata, description, example_block ) @example_group_instance = @exception = nil @clock = RSpec::Core::Time end
def instance_exec(*args, &block)
- Private: -
def instance_exec(*args, &block) @example_group_instance.instance_exec(*args, &block) end
def instance_exec_with_rescue(context, &block)
- Private: -
def instance_exec_with_rescue(context, &block) @example_group_instance.instance_exec(self, &block) rescue Exception => e set_exception(e, context) end
def mocks_need_verification?
def mocks_need_verification? exception.nil? || execution_result.pending_fixed? end
def record_finished(status)
def record_finished(status) execution_result.record_finished(status, clock.now) end
def run(example_group_instance, reporter)
-
example_group_instance
() -- the instance of an ExampleGroup subclass
Other tags:
- Api: - private
def run(example_group_instance, reporter) @example_group_instance = example_group_instance RSpec.current_example = self start(reporter) begin if skipped? Pending.mark_pending! self, skip elsif !RSpec.configuration.dry_run? with_around_example_hooks do begin run_before_example @example_group_instance.instance_exec(self, &@example_block) if pending? Pending.mark_fixed! self raise Pending::PendingExampleFixedError, 'Expected example to fail since it is pending, but it passed.', [location] end rescue Pending::SkipDeclaredInExample # no-op, required metadata has already been set by the `skip` # method. rescue Exception => e set_exception(e) ensure run_after_example 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 end finish(reporter) ensure RSpec.current_example = nil end
def run_after_example
def run_after_example @example_group_class.hooks.run(:after, :example, self) verify_mocks assign_generated_description if RSpec.configuration.expecting_with_rspec? rescue Exception => e set_exception(e, "in an `after(:example)` hook") ensure @example_group_instance.teardown_mocks_for_rspec end
def run_before_example
def run_before_example @example_group_instance.setup_mocks_for_rspec @example_group_class.hooks.run(:before, :example, self) end
def set_exception(exception, context=nil)
- Private: -
def set_exception(exception, context=nil) if pending? && !(Pending::PendingExampleFixedError === exception) execution_result.pending_exception = exception else if @exception # An error has already been set; we don't want to override it, # but we also don't want silence the error, so let's print it. msg = <<-EOS rror occurred #{context} exception.class}: #{exception.message} curred at #{exception.backtrace.first} EOS RSpec.configuration.reporter.message(msg) end @exception ||= exception end end
def skip_message
def skip_message if String === skip skip else Pending::NO_REASON_GIVEN end end
def skip_with_exception(reporter, exception)
- Private: -
def skip_with_exception(reporter, exception) start(reporter) Pending.mark_skipped! self, exception.argument finish(reporter) end
def start(reporter)
def start(reporter) reporter.example_started(self) execution_result.started_at = clock.now end
def verify_mocks
def verify_mocks @example_group_instance.verify_mocks_for_rspec if mocks_need_verification? rescue Exception => e if pending? execution_result.pending_fixed = false @exception = nil else set_exception(e) end end
def with_around_example_hooks(&block)
def with_around_example_hooks(&block) if around_example_hooks.empty? yield else @example_group_class.hooks.run(:around, :example, self, Procsy.new(self, &block)) end rescue Exception => e set_exception(e, "in an `around(:example)` hook") end