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.

Parameters:

  • example_group_class (Class)

    the subclass of ExampleGroup in which this Example is declared

  • description (String)

    the String passed to the it method (or alias)

  • user_metadata (Hash)

    additional args passed to it to be used as metadata

  • example_block (Proc) (defaults to: nil)

    the block of code that represents the example



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rspec/core/example.rb', line 127

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

  @metadata = Metadata::ExampleHash.create(
    @example_group_class., , description, example_block
  )

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

Instance Attribute Details

#exceptionvoid (readonly)

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



99
100
101
# File 'lib/rspec/core/example.rb', line 99

def exception
  @exception
end

#metadatavoid (readonly)

Returns the metadata object associated with this example.



104
105
106
# File 'lib/rspec/core/example.rb', line 104

def 
  @metadata
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.



141
142
143
# File 'lib/rspec/core/example.rb', line 141

def example_group
  @example_group_class
end

#execution_resultExecutionResult

Returns represents the result of running this example.

Returns:



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

 :execution_result

#file_pathString

Returns the relative path to the file where this example was defined.

Returns:

  • (String)

    the relative path to the file where this example was defined.



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

 :file_path

#full_descriptionString

Returns the full description (including the docstrings of all parent example groups).

Returns:

  • (String)

    the full description (including the docstrings of all parent example groups).



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

 :full_description

#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

Returns the exact source location of this example in a form like ./path/to/spec.rb:17

Returns:

  • (String)

    the exact source location of this example in a form like ./path/to/spec.rb:17



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

 :location

#pendingBoolean Also known as: pending?

Returns flag that indicates that the example is not expected to pass. It will be run and will either have a pending result (if a failure occurs) or a failed result (if no failure occurs).

Returns:

  • (Boolean)

    flag that indicates that the example is not expected to pass. It will be run and will either have a pending result (if a failure occurs) or a failed result (if no failure occurs).



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

 :pending

#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.

Parameters:

  • example_group_instance

    the instance of an ExampleGroup subclass



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rspec/core/example.rb', line 152

def run(example_group_instance, reporter)
  @example_group_instance = example_group_instance
  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_example_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
    ExampleGroup.instance_variables_for_example(@example_group_instance).each do |ivar|
      @example_group_instance.instance_variable_set(ivar, nil)
    end
    @example_group_instance = nil
  end

  finish(reporter)
ensure
  RSpec.current_example = nil
end

#skipBoolean Also known as: skipped?

Returns flag that will cause the example to not run. The ExecutionResult status will be :pending.

Returns:

  • (Boolean)

    flag that will cause the example to not run. The ExecutionResult status will be :pending.



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

 :skip