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. An instance of Example is returned by the example method available in examples, before and after hooks, and yielded to around hooks.

See Also:

Instance Attribute 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



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

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

#exceptionObject (readonly)

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



33
34
35
# File 'lib/rspec/core/example.rb', line 33

def exception
  @exception
end

#metadataObject (readonly)

Returns the metadata object associated with this example.



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

def 
  @metadata
end

Instance Method Details

#descriptionObject

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.



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

def description
  [:description].to_s.empty? ? "example at #{location}" : [:description]
end

#example_groupObject

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



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

def example_group
  @example_group_class
end

#optionsObject

Deprecated.

access options via metadata instead



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

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



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
107
108
109
110
111
112
113
# File 'lib/rspec/core/example.rb', line 76

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

  start(reporter)

  begin
    unless pending
      with_around_each_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