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. @see ExampleGroup
@attr_reader
Returns the first exception raised in the context of running this example (nil if no exception is raised)
@attr_reader @private
Returns the example_group_instance that provides the context for running this example.
@private
Used to define methods that delegate to this example’s metadata
# File lib/rspec/core/example.rb, line 14 14: def self.delegate_to_metadata(*keys) 15: keys.each { |key| define_method(key) { @metadata[key] } } 16: 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 52 52: def initialize(example_group_class, description, metadata, example_block=nil) 53: @example_group_class, @options, @example_block = example_group_class, metadata, example_block 54: @metadata = @example_group_class.metadata.for_example(description, metadata) 55: @exception = nil 56: @pending_declared_in_example = false 57: end
@private
Wraps the example block in a Proc so it can invoked using `run` or `call` in [around](../Hooks#around-instance_method) hooks.
# File lib/rspec/core/example.rb, line 119 119: def self.procsy(metadata, &proc) 120: proc.extend(Procsy).with(metadata) 121: end
@private
# File lib/rspec/core/example.rb, line 149 149: def all_apply?(filters) 150: @metadata.all_apply?(filters) || @example_group_class.all_apply?(filters) 151: end
@private
# File lib/rspec/core/example.rb, line 144 144: def any_apply?(filters) 145: metadata.any_apply?(filters) 146: end
@private
# File lib/rspec/core/example.rb, line 154 154: def around_each_hooks 155: @around_each_hooks ||= example_group.around_each_hooks_for(self) 156: end
Returns the string submitted to `example` or its aliases (e.g. `specify`, `it`, etc). If no string is submitted (e.g. `it { should do_something }`) it returns the message generated by the matcher if there is one, otherwise returns a message including the location of the example.
# File lib/rspec/core/example.rb, line 25 25: def description 26: metadata[:description].to_s.empty? ? "example at #{location}" : metadata[:description] 27: end
Returns the example group class that provides the context for running this example.
# File lib/rspec/core/example.rb, line 66 66: def example_group 67: @example_group_class 68: 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 170 170: def fail_with_exception(reporter, exception) 171: start(reporter) 172: set_exception(exception) 173: finish(reporter) 174: end
@private
# File lib/rspec/core/example.rb, line 177 177: def instance_eval(&block) 178: @example_group_instance.instance_eval(&block) 179: end
@private
# File lib/rspec/core/example.rb, line 187 187: def instance_eval_with_args(*args, &block) 188: @example_group_instance.instance_eval_with_args(*args, &block) 189: end
@private
# File lib/rspec/core/example.rb, line 182 182: def instance_eval_with_rescue(&block) 183: @example_group_instance.instance_eval_with_rescue(&block) 184: end
@deprecated access options via metadata instead
# File lib/rspec/core/example.rb, line 60 60: def options 61: @options 62: 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 76 76: def run(example_group_instance, reporter) 77: @example_group_instance = example_group_instance 78: @example_group_instance.example = self 79: 80: start(reporter) 81: 82: begin 83: unless pending 84: with_around_each_hooks do 85: begin 86: run_before_each 87: @example_group_instance.instance_eval(&@example_block) 88: rescue Pending::PendingDeclaredInExample => e 89: @pending_declared_in_example = e.message 90: rescue Exception => e 91: set_exception(e) 92: ensure 93: run_after_each 94: end 95: end 96: end 97: rescue Exception => e 98: set_exception(e) 99: ensure 100: @example_group_instance.instance_variables.each do |ivar| 101: @example_group_instance.instance_variable_set(ivar, nil) 102: end 103: @example_group_instance = nil 104: 105: begin 106: assign_auto_description 107: rescue Exception => e 108: set_exception(e) 109: end 110: end 111: 112: finish(reporter) 113: end
# File lib/rspec/core/example.rb, line 249 249: def assign_auto_description 250: return unless RSpec.configuration.expecting_with_rspec? 251: if metadata[:description].empty? and !pending? 252: metadata[:description] = RSpec::Matchers.generated_description 253: end 254: RSpec::Matchers.clear_generated_description 255: end
# File lib/rspec/core/example.rb, line 211 211: def finish(reporter) 212: if @exception 213: @exception.extend(NotPendingExampleFixed) unless @exception.respond_to?(:pending_fixed?) 214: record_finished 'failed', :exception => @exception 215: reporter.example_failed self 216: false 217: elsif @pending_declared_in_example 218: record_finished 'pending', :pending_message => @pending_declared_in_example 219: reporter.example_pending self 220: true 221: elsif pending 222: record_finished 'pending', :pending_message => String === pending ? pending : Pending::NO_REASON_GIVEN 223: reporter.example_pending self 224: true 225: else 226: record_finished 'passed' 227: reporter.example_passed self 228: true 229: end 230: end
# File lib/rspec/core/example.rb, line 257 257: def record(results={}) 258: execution_result.update(results) 259: end
# File lib/rspec/core/example.rb, line 232 232: def record_finished(status, results={}) 233: finished_at = Time.now 234: record results.merge(:status => status, :finished_at => finished_at, :run_time => (finished_at - execution_result[:started_at])) 235: end
# File lib/rspec/core/example.rb, line 242 242: def run_after_each 243: @example_group_class.run_after_each_hooks(self) 244: @example_group_instance.verify_mocks_for_rspec if @example_group_instance.respond_to?(:verify_mocks_for_rspec) 245: ensure 246: @example_group_instance.teardown_mocks_for_rspec if @example_group_instance.respond_to?(:teardown_mocks_for_rspec) 247: end
# File lib/rspec/core/example.rb, line 237 237: def run_before_each 238: @example_group_instance.setup_mocks_for_rspec if @example_group_instance.respond_to?(:setup_mocks_for_rspec) 239: @example_group_class.run_before_each_hooks(self) 240: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.