class RSpec::Core::Example
Experimental RBS support (using type sampling data from the type_fusion
project).
# sig/rspec/core/example.rbs class RSpec::Core::Example def finish: (RSpec::Core::Reporter reporter) -> true def hooks: () -> RSpec::Core::Hooks::HookCollections def id: () -> String def inspect_output: () -> String def mocks_need_verification?: () -> true def pending?: () -> false def start: (RSpec::Core::Reporter reporter) -> Time def verify_mocks: () -> untyped end
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 inadvertently 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 self.parse_id(id)
- Private: -
def self.parse_id(id) # http://rubular.com/r/OMZSAPcAfn id.match(/\A(.*?)(?:\[([\d\s:,]+)\])?\z/).captures end
def assign_generated_description
def assign_generated_description if metadata[:description].empty? && (description = generate_description) metadata[:description] = description metadata[:full_description] += description end ensure RSpec::Matchers.clear_generated_description end
def description
by the matcher if there is one, otherwise returns a message including
`it { is_expected.to do_something }`) it returns the message generated
`specify`, `it`, etc). If no string is submitted (e.g.
Returns the string submitted to `example` or its aliases (e.g.
def description description = if metadata[:description].to_s.empty? location_description else metadata[:description] end RSpec.configuration.format_docstrings_block.call(description) end
def display_exception
- Private: -
def display_exception @exception || execution_result.pending_exception end
def display_exception=(ex)
- Private: -
def display_exception=(ex) if pending? && !(Pending::PendingExampleFixedError === ex) @exception = nil execution_result.pending_fixed = false execution_result.pending_exception = ex else @exception = ex end end
def duplicate_with(metadata_overrides={})
-
(Example)
- a duplicate of the example with modified metadata
Parameters:
-
metadata_overrides
(Hash
) -- the hash to override the example metadata
def duplicate_with(metadata_overrides={}) new_metadata = metadata.clone.merge(metadata_overrides) RSpec::Core::Metadata::RESERVED_KEYS.each do |reserved_key| new_metadata.delete reserved_key end # don't clone the example group because the new example # must belong to the same example group (not a clone). # # block is nil in new_metadata so we have to get it from metadata. Example.new(example_group, description.clone, new_metadata, metadata[:block]) 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)
Experimental RBS support (using type sampling data from the type_fusion
project).
def finish: (RSpec::Core::Reporter reporter) -> true
This signature was generated using 1 sample from 1 application.
def finish(reporter) pending_message = execution_result.pending_message if @exception execution_result.exception = @exception record_finished :failed, reporter reporter.example_failed self false elsif pending_message execution_result.pending_message = pending_message record_finished :pending, reporter reporter.example_pending self true else record_finished :passed, reporter reporter.example_passed self true end end
def generate_description
def generate_description RSpec::Matchers.generated_description rescue Support::AllExceptionsExceptOnesWeMustNotRescue => e location_description + " (Got an error when generating description " \ "from matcher: #{e.class}: #{e.message} -- #{e.backtrace.first})" end
def hooks
Experimental RBS support (using type sampling data from the type_fusion
project).
def hooks: () -> RSpec::Core::Hooks::HookCollections
This signature was generated using 1 sample from 1 application.
def hooks example_group_instance.singleton_class.hooks end
def id
Experimental RBS support (using type sampling data from the type_fusion
project).
def id: () -> String
This signature was generated using 1 sample from 1 application.
-
(String)
- the unique id of this example. Pass
def id @id ||= Metadata.id_from(metadata) 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 -
user_metadata
(Hash
) -- additional args passed to `it` to be used as -
description
(String
) -- the String passed to the `it` method (or -
example_group_class
(Class
) -- the subclass of ExampleGroup in which
def initialize(example_group_class, description, user_metadata, example_block=nil) @example_group_class = example_group_class @example_block = example_block # Register the example with the group before creating the metadata hash. # This is necessary since creating the metadata hash triggers # `when_first_matching_example_defined` callbacks, in which users can # load RSpec support code which defines hooks. For that to work, the # examples and example groups must be registered at the time the # support code is called or be defined afterwards. # Begin defined beforehand but registered afterwards causes hooks to # not be applied where they should. example_group_class.examples << self @metadata = Metadata::ExampleHash.create( @example_group_class.metadata, user_metadata, example_group_class.method(:next_runnable_index_for), description, example_block ) config = RSpec.configuration config.apply_derived_metadata_to(@metadata) # This should perhaps be done in `Metadata::ExampleHash.create`, # but the logic there has no knowledge of `RSpec.world` and we # want to keep it that way. It's easier to just assign it here. @metadata[:last_run_status] = config.last_run_statuses[id] @example_group_instance = @exception = nil @clock = RSpec::Core::Time @reporter = RSpec::Core::NullReporter end
def inspect
def inspect "#<#{self.class.name} #{description.inspect}>" end
def inspect_output
Experimental RBS support (using type sampling data from the type_fusion
project).
def inspect_output: () -> String
This signature was generated using 1 sample from 1 application.
def inspect_output inspect_output = "\"#{description}\"" unless metadata[:description].to_s.empty? inspect_output += " (#{location})" end inspect_output end
def instance_exec(*args, &block)
- Private: -
def instance_exec(*args, &block) @example_group_instance.instance_exec(*args, &block) end
def location_description
def location_description "example at #{location}" end
def location_rerun_argument
def location_rerun_argument @location_rerun_argument ||= begin loaded_spec_files = RSpec.configuration.loaded_spec_files Metadata.ascending(metadata) do |meta| return meta[:location] if loaded_spec_files.include?(meta[:absolute_file_path]) end end end
def mocks_need_verification?
Experimental RBS support (using type sampling data from the type_fusion
project).
def mocks_need_verification?: () -> true
This signature was generated using 1 sample from 1 application.
def mocks_need_verification? exception.nil? || execution_result.pending_fixed? end
def pending?
Experimental RBS support (using type sampling data from the type_fusion
project).
def pending?: () -> false
This signature was generated using 1 sample from 1 application.
def pending? !!pending end
def record_finished(status, reporter)
def record_finished(status, reporter) execution_result.record_finished(status, clock.now) reporter.example_finished(self) end
def rerun_argument
- Note: - If there are multiple examples identified by this location, they will use {#id}
Deprecated:
- Use {#location_rerun_argument} instead.
def rerun_argument location_rerun_argument 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 @reporter = reporter RSpec.configuration.configure_example(self, hooks) RSpec.current_example = self start(reporter) Pending.mark_pending!(self, pending) if pending? begin if skipped? Pending.mark_pending! self, skip elsif !RSpec.configuration.dry_run? with_around_and_singleton_context_hooks do begin run_before_example RSpec.current_scope = :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 => _ # The "=> _" is normally useless but on JRuby it is a workaround # for a bug that prevents us from getting backtraces: # https://github.com/jruby/jruby/issues/4467 # # no-op, required metadata has already been set by the `skip` # method. rescue AllExceptionsExcludingDangerousOnesOnRubiesThatAllowIt => e set_exception(e) ensure RSpec.current_scope = :after_example_hook run_after_example end end end rescue Support::AllExceptionsExceptOnesWeMustNotRescue => e set_exception(e) ensure @example_group_instance = nil # if you love something... let it go end finish(reporter) ensure execution_result.ensure_timing_set(clock) RSpec.current_example = nil end
def run_after_example
def run_after_example assign_generated_description if defined?(::RSpec::Matchers) hooks.run(:after, :example, self) verify_mocks ensure @example_group_instance.teardown_mocks_for_rspec end
def run_before_example
def run_before_example @example_group_instance.setup_mocks_for_rspec hooks.run(:before, :example, self) end
def set_aggregate_failures_exception(exception)
- Private: -
def set_aggregate_failures_exception(exception) return set_exception(exception) unless display_exception exception = RSpec::Core::MultipleExceptionError::InterfaceTag.for(exception) exception.add display_exception self.display_exception = exception end
def set_exception(exception)
- Private: -
def set_exception(exception) return self.display_exception = exception unless display_exception unless RSpec::Core::MultipleExceptionError === display_exception self.display_exception = RSpec::Core::MultipleExceptionError.new(display_exception) end display_exception.add exception 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 skipped?
def skipped? !!skip end
def start(reporter)
Experimental RBS support (using type sampling data from the type_fusion
project).
def start: (RSpec::Core::Reporter reporter) -> Time
This signature was generated using 1 sample from 1 application.
def start(reporter) reporter.example_started(self) execution_result.started_at = clock.now end
def update_inherited_metadata(updates)
- Private: -
def update_inherited_metadata(updates) metadata.update(updates) do |_key, existing_example_value, _new_inherited_value| existing_example_value end end
def verify_mocks
Experimental RBS support (using type sampling data from the type_fusion
project).
def verify_mocks: () -> untyped
This signature was generated using 2 samples from 1 application.
def verify_mocks @example_group_instance.verify_mocks_for_rspec if mocks_need_verification? rescue Support::AllExceptionsExceptOnesWeMustNotRescue => e set_exception(e) end
def with_around_and_singleton_context_hooks
def with_around_and_singleton_context_hooks singleton_context_hooks_host = example_group_instance.singleton_class singleton_context_hooks_host.run_before_context_hooks(example_group_instance) with_around_example_hooks { yield } ensure singleton_context_hooks_host.run_after_context_hooks(example_group_instance) end
def with_around_example_hooks
def with_around_example_hooks RSpec.current_scope = :before_example_hook hooks.run(:around, :example, self) { yield } rescue Support::AllExceptionsExceptOnesWeMustNotRescue => e set_exception(e) end