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

Wrapper for an instance of a subclass of ExampleGroup. An instance of RSpec::Core::Example is returned by the example method exposed to examples, before and after hooks, and yielded to around hooks.

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.

of an ExampleGroup, not in the context of an instance of Example.

Examples:


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

  config.after do
    log example.description
  end

  config.around do |ex|
    log example.description
    ex.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

- (Example) initialize(example_group_class, description, user_metadata, example_block = nil)

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



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

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

- (void) exception (readonly)

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



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

def exception
  @exception
end

- (void) metadata (readonly)

Returns the metadata object associated with this example.



74
75
76
# File 'lib/rspec/core/example.rb', line 74

def 
  @metadata
end

Instance Method Details

- (void) description

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.



58
59
60
61
62
63
# File 'lib/rspec/core/example.rb', line 58

def description
  description = [:description].to_s.empty? ?
    "example at #{location}" :
    [:description]
  RSpec.configuration.format_docstrings_block.call(description)
end

- (void) example_group

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



107
108
109
# File 'lib/rspec/core/example.rb', line 107

def example_group
  @example_group_class
end

- (void) run(example_group_instance, reporter)

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rspec/core/example.rb', line 118

def run(example_group_instance, reporter)
  @example_group_instance = example_group_instance
  RSpec.current_example = self

  start(reporter)

  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
    @example_group_instance.instance_variables.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