Class: RSpec::Core::Example

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/core/example.rb

Overview

Note:

Example blocks are evaluated in the context of an instance of an ExampleGroup, not in the context of an instance of Example.

Wrapper for an instance of a subclass of ExampleGroup. An instance of RSpec::Core::Example is returned by example definition methods such as it and is yielded to the it, before, after, around, let and subject blocks.

This allows us to provide rich metadata about each individual example without adding tons of methods directly to the ExampleGroup that users may inadvertantly redefine.

Useful for configuring logging and/or taking some action based on the state of an example's metadata.

Examples:


RSpec.configure do |config|
  config.before do |example|
    log example.description
  end

  config.after do |example|
    log example.description
  end

  config.around do |example|
    log example.description
    example.run
  end
end

shared_examples "auditable" do
  it "does something" do
    log "#{example.full_description}: #{auditable.inspect}"
    auditable.should do_something
  end
end

See Also:

Defined Under Namespace

Classes: ExecutionResult, Procsy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example_group_class, description, user_metadata, example_block = nil) ⇒ Example

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new instance of Example.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rspec/core/example.rb', line 153

def initialize(example_group_class, description, , example_block=nil)
  @example_group_class = example_group_class
  @example_block       = example_block

   = ::ExampleHash.create(
    @example_group_class., ,
    example_group_class.method(:next_runnable_index_for),
    description, example_block
  )

  # 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.
  [:last_run_status] = RSpec.configuration.last_run_statuses[id]

  @example_group_instance = @exception = nil
  @clock = RSpec::Core::Time
  @reporter = RSpec::Core::NullReporter
end

Instance Attribute Details

#exceptionvoid (readonly)

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



125
126
127
# File 'lib/rspec/core/example.rb', line 125

def exception
  @exception
end

#metadatavoid (readonly)

Returns the metadata object associated with this example.



130
131
132
# File 'lib/rspec/core/example.rb', line 130

def 
  
end

#reporterRSpec::Core::Reporter (readonly)



174
175
176
# File 'lib/rspec/core/example.rb', line 174

def reporter
  @reporter
end

Instance Method Details

#descriptionvoid

Returns the string submitted to example or its aliases (e.g. specify, it, etc). If no string is submitted (e.g. it { is_expected.to do_something }) it returns the message generated by the matcher if there is one, otherwise returns a message including the location of the example.



76
77
78
79
80
81
82
83
84
# File 'lib/rspec/core/example.rb', line 76

def description
  description = if [:description].to_s.empty?
                  location_description
                else
                  [:description]
                end

  RSpec.configuration.format_docstrings_block.call(description)
end

#example_groupvoid

Returns the example group class that provides the context for running this example.



178
179
180
# File 'lib/rspec/core/example.rb', line 178

def example_group
  @example_group_class
end

#execution_resultExecutionResult



53
# File 'lib/rspec/core/example.rb', line 53

 :execution_result

#file_pathString



56
# File 'lib/rspec/core/example.rb', line 56

 :file_path

#full_descriptionString



59
# File 'lib/rspec/core/example.rb', line 59

 :full_description

#idString



117
118
119
# File 'lib/rspec/core/example.rb', line 117

def id
  @id ||= .id_from()
end

#inspect_outputvoid

Returns a description of the example that always includes the location.



87
88
89
90
91
92
93
# File 'lib/rspec/core/example.rb', line 87

def inspect_output
  inspect_output = "\"#{description}\""
  unless [:description].to_s.empty?
    inspect_output << " (#{location})"
  end
  inspect_output
end

#locationString



62
# File 'lib/rspec/core/example.rb', line 62

 :location

#location_rerun_argumentvoid

Returns the location-based argument that can be passed to the rspec command to rerun this example.



96
97
98
99
100
101
102
103
104
# File 'lib/rspec/core/example.rb', line 96

def location_rerun_argument
  @location_rerun_argument ||= begin
    loaded_spec_files = RSpec.configuration.loaded_spec_files

    .ascending() do |meta|
      return meta[:location] if loaded_spec_files.include?(meta[:absolute_file_path])
    end
  end
end

#pendingBoolean Also known as: pending?



66
# File 'lib/rspec/core/example.rb', line 66

 :pending

#rerun_argumentvoid

Deprecated.
Note:

If there are multiple examples identified by this location, they will use #id to rerun instead, but this method will still return the location (that's why it is deprecated!).

Returns the location-based argument that can be passed to the rspec command to rerun this example.



111
112
113
# File 'lib/rspec/core/example.rb', line 111

def rerun_argument
  location_rerun_argument
end

#run(example_group_instance, reporter) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

instance_execs the block passed to the constructor in the context of the instance of RSpec::Core::ExampleGroup.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rspec/core/example.rb', line 189

def run(example_group_instance, reporter)
  @example_group_instance = example_group_instance
  @reporter = reporter
  hooks.register_global_singleton_context_hooks(self, RSpec.configuration.hooks)
  RSpec.configuration.configure_example(self)
  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
          @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 = nil # if you love something... let it go
  end

  finish(reporter)
ensure
  RSpec.current_example = nil
end

#skipBoolean Also known as: skipped?



69
# File 'lib/rspec/core/example.rb', line 69

 :skip