Class: RSpec::Core::Example

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

Overview

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 Also:

Defined Under Namespace

Modules: NotPendingExampleFixed, Procsy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new instance of Example.

Parameters:

  • example_group_class

    the subclass of ExampleGroup in which this Example is declared

  • description

    the String passed to the `it` method (or alias)

  • metadata

    additional args passed to `it` to be used as metadata

  • example_block (defaults to: nil)

    the block of code that represents the example


45
46
47
48
49
50
# File 'lib/rspec/core/example.rb', line 45

def initialize(example_group_class, description, , example_block=nil)
  @example_group_class, @options, @example_block = example_group_class, , example_block
  @metadata  = @example_group_class..for_example(description, )
  @exception = nil
  @pending_declared_in_example = false
end

Instance Attribute Details

#example_group_instanceObject (readonly)

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


38
39
40
# File 'lib/rspec/core/example.rb', line 38

def example_group_instance
  @example_group_instance
end

#exceptionObject (readonly)

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


26
27
28
# File 'lib/rspec/core/example.rb', line 26

def exception
  @exception
end

#metadataObject (readonly)

Returns the metadata object associated with this example.


31
32
33
# File 'lib/rspec/core/example.rb', line 31

def 
  @metadata
end

Class Method Details

.delegate_to_metadata(*keys) ⇒ Object

Used to define methods that delegate to this example's metadata


14
15
16
17
18
# File 'lib/rspec/core/example.rb', line 14

def self.(*keys)
  keys.each do |key|
    define_method(key) {@metadata[key]}
  end
end

.procsy(metadata, &proc) ⇒ Object

Wraps the example block in a Proc so it can invoked using `run` or `call` in [around](../Hooks#around-instance_method) hooks.


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

def self.procsy(, &proc)
  Proc.new(&proc).extend(Procsy).with()
end

Instance Method Details

#all_apply?(filters) ⇒ Boolean

Returns:

  • (Boolean)

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

def all_apply?(filters)
  @metadata.all_apply?(filters) || @example_group_class.all_apply?(filters)
end

#any_apply?(filters) ⇒ Boolean

Returns:

  • (Boolean)

137
138
139
# File 'lib/rspec/core/example.rb', line 137

def any_apply?(filters)
  .any_apply?(filters)
end

#around_hooksObject


147
148
149
# File 'lib/rspec/core/example.rb', line 147

def around_hooks
  @around_hooks ||= example_group.around_hooks_for(self)
end

#example_groupObject

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


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

def example_group
  @example_group_class
end

#fail_with_exception(reporter, exception) ⇒ Object

Used internally to set an exception and fail without actually executing the example when an exception is raised in before(:all).


163
164
165
166
167
# File 'lib/rspec/core/example.rb', line 163

def fail_with_exception(reporter, exception)
  start(reporter)
  set_exception(exception)
  finish(reporter)
end

#optionsObject

Deprecated.

access options via metadata instead


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

def options
  @options
end

#run(example_group_instance, reporter) ⇒ Object

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_evals the block submitted to the constructor in the context of the instance of ExampleGroup

Parameters:

  • example_group_instance

    the instance of an ExampleGroup subclass


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rspec/core/example.rb', line 69

def run(example_group_instance, reporter)
  @example_group_instance = example_group_instance
  @example_group_instance.example = self

  start(reporter)

  begin
    unless pending
      with_around_hooks do
        begin
          run_before_each
          @example_group_instance.instance_eval(&@example_block)
        rescue Pending::PendingDeclaredInExample => e
          @pending_declared_in_example = e.message
        rescue Exception => e
          set_exception(e)
        ensure
          run_after_each
        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

    begin
      assign_auto_description
    rescue Exception => e
      set_exception(e)
    end
  end

  finish(reporter)
end

#set_exception(exception) ⇒ Object

Used internally to set an exception in an after hook, which captures the exception but doesn't raise it.


155
156
157
# File 'lib/rspec/core/example.rb', line 155

def set_exception(exception)
  @exception ||= exception
end