class RSpec::Core::Example
@see ExampleGroup
end
end
auditable.should do_something
log “#{example.full_description}: #{auditable.inspect}”
it “does something” do
shared_examples “auditable” do
end
end
ex.run
log example.description
config.around do |ex|
end
log example.description
config.after do
end
log example.description
config.before do
RSpec.configure do |config|
@example
on the state of an example’s metadata.
Useful for configuring logging and/or taking some action based
and yielded to {Hooks#around around} hooks.
exposed to examples, {Hooks#before before} and {Hooks#after after} hooks,
‘Example` is returned by the {ExampleGroup#example example} method
Wrapper for an instance of a subclass of {ExampleGroup}. An instance of
def self.delegate_to_metadata(*keys)
- Private: -
def self.delegate_to_metadata(*keys) keys.each { |key| define_method(key) { @metadata[key] } } end
def self.procsy(metadata, &proc)
- Api: - private
def self.procsy(metadata, &proc) proc.extend(Procsy).with(metadata) end
def all_apply?(filters)
- Private: -
def all_apply?(filters) @metadata.all_apply?(filters) || @example_group_class.all_apply?(filters) end
def any_apply?(filters)
- Private: -
def any_apply?(filters) metadata.any_apply?(filters) end
def around_each_hooks
- Private: -
def around_each_hooks @around_each_hooks ||= example_group.around_each_hooks_for(self) end
def assign_generated_description
def assign_generated_description return unless RSpec.configuration.expecting_with_rspec? if metadata[:description_args].empty? and !pending? metadata[:description_args] << RSpec::Matchers.generated_description end 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) if @exception record_finished 'failed', :exception => @exception reporter.example_failed self false elsif @pending_declared_in_example record_finished 'pending', :pending_message => @pending_declared_in_example reporter.example_pending self true elsif pending record_finished 'pending', :pending_message => String === pending ? pending : Pending::NO_REASON_GIVEN reporter.example_pending self true else record_finished 'passed' reporter.example_passed self true end end
def initialize(example_group_class, description, metadata, example_block=nil)
-
example_block
() -- the block of code that represents the example
-
metadata
() -- additional args passed to `it` to be used as metadata
-
description
() -- the String passed to the `it` method (or alias)
-
example_group_class
() -- the subclass of ExampleGroup in which this Example is declared
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) @example_group_instance = @exception = nil @pending_declared_in_example = false end
def instance_eval_with_args(*args, &block)
- Private: -
def instance_eval_with_args(*args, &block) @example_group_instance.instance_eval_with_args(*args, &block) end
def instance_eval_with_rescue(context = nil, &block)
- Private: -
def instance_eval_with_rescue(context = nil, &block) @example_group_instance.instance_eval_with_rescue(self, context, &block) end
def options
- access options via metadata instead
def options RSpec.deprecate("`RSpec::Core::Example#options`", :replacement => "`RSpec::Core::Example#metadata`") @options end
def record(results={})
def record(results={}) execution_result.update(results) end
def record_finished(status, results={})
def record_finished(status, results={}) finished_at = RSpec::Core::Time.now record results.merge(:status => status, :finished_at => finished_at, :run_time => (finished_at - execution_result[:started_at]).to_f) 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 unless pending with_around_each_hooks do begin run_before_each @example_group_instance.instance_eval_with_args(self, &@example_block) rescue Pending::SkipDeclaredInExample => 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_generated_description rescue Exception => e set_exception(e, "while assigning the example description") end end finish(reporter) ensure RSpec.current_example = nil end
def run_after_each
def run_after_each @example_group_class.run_after_each_hooks(self) verify_mocks rescue Exception => e set_exception(e, "in an after(:each) hook") ensure @example_group_instance.teardown_mocks_for_rspec end
def run_before_each
def run_before_each @example_group_instance.setup_mocks_for_rspec @example_group_class.run_before_each_hooks(self) end
def set_exception(exception, context=nil)
- Private: -
def set_exception(exception, context=nil) if @exception && context != :dont_print # 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 or occurred #{context} ception.class}: #{exception.message} rred at #{exception.backtrace.first} EOS RSpec.configuration.reporter.message(msg) end @exception ||= exception end
def start(reporter)
def start(reporter) reporter.example_started(self) record :started_at => RSpec::Core::Time.now end
def verify_mocks
def verify_mocks @example_group_instance.verify_mocks_for_rspec rescue Exception => e set_exception(e, :dont_print) end
def with_around_each_hooks(&block)
def with_around_each_hooks(&block) if around_each_hooks.empty? yield else @example_group_class.run_around_each_hooks(self, Example.procsy(metadata, &block)) end rescue Exception => e set_exception(e, "in an around(:each) hook") end