Parent

Class Index [+]

Quicksearch

RSpec::Core::Example

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

Attributes

exception[R]

@attr_reader

Returns the first exception raised in the context of running this example (nil if no exception is raised)

metadata[R]

@attr_reader

Returns the metadata object associated with this example.

example_group_instance[R]

@attr_reader @private

Returns the example_group_instance that provides the context for running this example.

Public Class Methods

delegate_to_metadata(*keys) click to toggle source

@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
new(example_group_class, description, metadata, example_block=nil) click to toggle source

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
procsy(metadata, &proc) click to toggle source

@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

Public Instance Methods

all_apply?(filters) click to toggle source

@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
any_apply?(filters) click to toggle source

@private

     # File lib/rspec/core/example.rb, line 144
144:       def any_apply?(filters)
145:         metadata.any_apply?(filters)
146:       end
around_each_hooks() click to toggle source

@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
description() click to toggle source

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
example_group() click to toggle source

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
fail_with_exception(reporter, exception) click to toggle source

@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
instance_eval(&block) click to toggle source

@private

     # File lib/rspec/core/example.rb, line 177
177:       def instance_eval(&block)
178:         @example_group_instance.instance_eval(&block)
179:       end
instance_eval_with_args(*args, &block) click to toggle source

@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
instance_eval_with_rescue(&block) click to toggle source

@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
options() click to toggle source

@deprecated access options via metadata instead

    # File lib/rspec/core/example.rb, line 60
60:       def options
61:         @options
62:       end
run(example_group_instance, reporter) click to toggle source

@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
set_exception(exception) click to toggle source

@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 162
162:       def set_exception(exception)
163:         @exception ||= exception
164:       end

Private Instance Methods

assign_auto_description() click to toggle source
     # 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
finish(reporter) click to toggle source
     # 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
record(results={}) click to toggle source
     # File lib/rspec/core/example.rb, line 257
257:       def record(results={})
258:         execution_result.update(results)
259:       end
record_finished(status, results={}) click to toggle source
     # 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
run_after_each() click to toggle source
     # 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
run_before_each() click to toggle source
     # 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
start(reporter) click to toggle source
     # File lib/rspec/core/example.rb, line 201
201:       def start(reporter)
202:         reporter.example_started(self)
203:         record :started_at => Time.now
204:       end
with_around_each_hooks(&block) click to toggle source
     # File lib/rspec/core/example.rb, line 193
193:       def with_around_each_hooks(&block)
194:         if around_each_hooks.empty?
195:           yield
196:         else
197:           @example_group_class.run_around_each_hooks(self, Example.procsy(metadata, &block))
198:         end
199:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.