123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Core::ExampleGroup

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
RSpec::Core::AnonymousExampleGroup
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Object
Defined in: rspec-core/lib/rspec/core/example_group.rb

Overview

ExampleGroup and Example are the main structural elements of rspec-core. Consider this example:

RSpec.describe Thing do
  it "does something" do
  end
end

The object returned by ‘describe Thing` is a subclass of ExampleGroup. The object returned by `it “does something”` is an instance of Example, which serves as a wrapper for an instance of the ExampleGroup in which it is declared.

Example group bodies (e.g. ‘describe` or context blocks) are evaluated in the context of a new subclass of ExampleGroup. Individual examples are evaluated in the context of an instance of the specific ExampleGroup subclass to which they belong.

Besides the class methods defined here, there are other interesting macros defined in Hooks, MemoizedHelpers::ClassMethods and SharedExampleGroup. There are additional instance methods available to your examples defined in MemoizedHelpers and Pending.

Constant Summary

Pending - Included

NOT_YET_IMPLEMENTED, NO_REASON_GIVEN

Metadata

Defining Examples

  • .define_example_method(name, extra_options = {}) Internal use only Internal use only

    @example.

  • .example

    Defines an example within a group.

  • .fexample

    Shortcut to define an example with ‘:focus => true`.

  • .fit

    Shortcut to define an example with ‘:focus => true`.

  • .focus

    Shortcut to define an example with ‘:focus => true`.

  • .fspecify

    Shortcut to define an example with ‘:focus => true`.

  • .it

    Defines an example within a group.

  • .pending

    Shortcut to define an example with ‘:pending => true`.

  • .skip

    Shortcut to define an example with ‘:skip => true`.

  • .specify

    Defines an example within a group.

  • .xexample

    Shortcut to define an example with ‘:skip => ’Temporarily skipped with xexample’‘.

  • .xit

    Shortcut to define an example with ‘:skip => ’Temporarily skipped with xit’‘.

  • .xspecify

    Shortcut to define an example with ‘:skip => ’Temporarily skipped with xspecify’‘.

Defining Example Groups

Including Shared Example Groups

Class Attribute Summary

Class Method Summary

SharedExampleGroup - Extended

MemoizedHelpers::ClassMethods - Extended

let

Generates a method whose return value is memoized after the first call.

let!

Just like ‘let`, except the block is invoked by an implicit before hook.

subject

Declares a ‘subject` for an example group which can then be wrapped with expect using is_expected to make it the target of an expectation in a concise, one-line example.

subject!

Just like ‘subject`, except the block is invoked by an implicit before hook.

Hooks - Extended

after

Declare a block of code to be run after each example (using ‘:example`) or once after all examples n the context (using :context).

append_after

Adds ‘block` to the back of the list of after blocks in the same scope (`:example`, :context, or :suite).

append_before

Alias for Hooks#before.

around

Declare a block of code, parts of which will be run before and parts after the example.

before

Declare a block of code to be run before each example (using ‘:example`) or once before any example (using :context).

hooks

Holds the various registered hooks.

prepend_after

Alias for Hooks#after.

prepend_before

Adds ‘block` to the front of the list of before blocks in the same scope (`:example`, :context, or :suite).

Instance Attribute Summary

MemoizedHelpers - Included

#__memoized

should just be placed in private section, but Ruby issues warnings on private attributes.

Instance Method Summary

Pending - Included

#pending

Marks an example as pending.

#skip

Marks an example as pending and skips execution.

MemoizedHelpers - Included

#is_expected

Wraps the ‘subject` in expect to make it the target of an expectation.

#should

When ‘should` is called with no explicit receiver, the call is delegated to the object returned by subject.

#should_not

Just like ‘should`, should_not delegates to the subject (implicit or explicit) of the example group.

#subject, #__init_memoized, #enforce_value_expectation, #initialize, #matcher_supports_value_expectations?

Class Attribute Details

.currently_executing_a_context_hook?Boolean (readonly)

Returns true if a ‘before(:context)` or after(:context) hook is currently executing.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 542

def self.currently_executing_a_context_hook?
  @currently_executing_a_context_hook
end

.top_level?Boolean (readonly)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 513

def self.top_level?
  superclass == ExampleGroup
end

Class Method Details

.add_example(example)

Adds an example to the example group

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 367

def self.add_example(example)
  reset_memoized
  examples << example
end

.before_context_ivars

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 529

def self.before_context_ivars
  @before_context_ivars ||= {}
end

.children

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 466

def self.children
  @children ||= []
end

.declaration_locations

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 667

def self.declaration_locations
  @declaration_locations ||= [Metadata.location_tuple_from()] +
    examples.map { |e| Metadata.location_tuple_from(e.) } +
    FlatMap.flat_map(children, &:declaration_locations)
end

.define_example_group_method(name, metadata = {})

This method is for internal use only.

See Also:

  • DSL#describe
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 246

def self.define_example_group_method(name, ={})
  idempotently_define_singleton_method(name) do |*args, &example_group_block|
    thread_data = RSpec::Support.thread_local_data
    top_level   = self == ExampleGroup

    registration_collection =
      if top_level
        if thread_data[:in_example_group]
          raise "Creating an isolated context from within a context is " \
                "not allowed. Change `RSpec.#{name}` to `#{name}` or " \
                "move this to a top-level scope."
        end

        thread_data[:in_example_group] = true
        RSpec.world.example_groups
      else
        children
      end

    begin
      description = args.shift
       = .dup
      .merge!(args.pop) if args.last.is_a? Hash
      args << 

      subclass(self, description, args, registration_collection, &example_group_block)
    ensure
      thread_data.delete(:in_example_group) if top_level
    end
  end

  RSpec::Core::DSL.expose_example_group_alias(name)
end

.define_example_method(name, extra_options = {})

This method is for internal use only.

@example

$1 "does something", :slow, :load_factor => 100 do
end
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 145

def self.define_example_method(name, extra_options={})
  idempotently_define_singleton_method(name) do |*all_args, &block|
    desc, *args = *all_args

    options = Metadata.build_hash_from(args)
    options.update(:skip => RSpec::Core::Pending::NOT_YET_IMPLEMENTED) unless block
    options.update(extra_options)

    RSpec::Core::Example.new(self, desc, options, block)
  end
end

.define_nested_shared_group_method(new_name, report_label = "it should behave like")

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 317

def self.define_nested_shared_group_method(new_name, report_label="it should behave like")
  idempotently_define_singleton_method(new_name) do |name, *args, &customization_block|
    # Pass :caller so the :location metadata is set properly.
    # Otherwise, it'll be set to the next line because that's
    # the block's source_location.
    group = example_group("#{report_label} #{name}", :caller => (the_caller = caller)) do
      find_and_eval_shared("examples", name, the_caller.first, *args, &customization_block)
    end
    group.[:shared_group_name] = name
    group
  end
end

.delegate_to_metadata(*names)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 76

def self.(*names)
  names.each do |name|
    idempotently_define_singleton_method(name) { .fetch(name) }
  end
end

.descendant_filtered_examples

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 460

def self.descendant_filtered_examples
  @descendant_filtered_examples ||= filtered_examples +
    FlatMap.flat_map(children, &:descendant_filtered_examples)
end

.descendants

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 503

def self.descendants
  @_descendants ||= [self] + FlatMap.flat_map(children, &:descendants)
end

.descriptionString

Returns:

  • (String)

    the current example group description

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 85

def self.description
  description = [:description]
  RSpec.configuration.format_docstrings_block.call(description)
end

.each_instance_variable_for_example(group)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 700

def self.each_instance_variable_for_example(group)
  group.instance_variables.each do |ivar|
    yield ivar unless ivar == INSTANCE_VARIABLE_TO_IGNORE
  end
end

.ensure_example_groups_are_configured

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 518

def self.ensure_example_groups_are_configured
  unless defined?(@@example_groups_configured)
    RSpec.configuration.configure_mock_framework
    RSpec.configuration.configure_expectation_framework
    # rubocop:disable Style/ClassVars
    @@example_groups_configured = true
    # rubocop:enable Style/ClassVars
  end
end

.example .example(&example_implementation) .example(doc_string, *metadata) .example(doc_string, *metadata, &example_implementation)

Defines an example within a group.

Examples:

example do
end

example "does something" do
end

example "does something", :slow, :uses_js do
end

example "does something", :with => 'additional metadata' do
end

example "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

Overloads:

  • .example(&example_implementation)

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .example(doc_string, *metadata)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

  • .example(doc_string, *metadata, &example_implementation)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 158

define_example_method :example

.examples

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 450

def self.examples
  @examples ||= []
end

.fexample .fexample(&example_implementation) .fexample(doc_string, *metadata) .fexample(doc_string, *metadata, &example_implementation)

Shortcut to define an example with ‘:focus => true`.

Examples:

fexample do
end

fexample "does something" do
end

fexample "does something", :slow, :uses_js do
end

fexample "does something", :with => 'additional metadata' do
end

fexample "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

Overloads:

  • .fexample(&example_implementation)

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .fexample(doc_string, *metadata)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

  • .fexample(doc_string, *metadata, &example_implementation)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 177

define_example_method :fexample, :focus => true

.filtered_examples

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 455

def self.filtered_examples
  RSpec.world.filtered_examples[self]
end

.find_and_eval_shared(label, name, inclusion_location, *args, &customization_block)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 379

def self.find_and_eval_shared(label, name, inclusion_location, *args, &customization_block)
  shared_module = RSpec.world.shared_example_group_registry.find(parent_groups, name)

  unless shared_module
    raise ArgumentError, "Could not find shared #{label} #{name.inspect}"
  end

  shared_module.include_in(
    self, Metadata.relative_path(inclusion_location),
    args, customization_block
  )
end

.fit .fit(&example_implementation) .fit(doc_string, *metadata) .fit(doc_string, *metadata, &example_implementation)

Shortcut to define an example with ‘:focus => true`.

Examples:

fit do
end

fit "does something" do
end

fit "does something", :slow, :uses_js do
end

fit "does something", :with => 'additional metadata' do
end

fit "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

Overloads:

  • .fit(&example_implementation)

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .fit(doc_string, *metadata)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

  • .fit(doc_string, *metadata, &example_implementation)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 180

define_example_method :fit,      :focus => true

.focus .focus(&example_implementation) .focus(doc_string, *metadata) .focus(doc_string, *metadata, &example_implementation)

Shortcut to define an example with ‘:focus => true`.

Examples:

focus do
end

focus "does something" do
end

focus "does something", :slow, :uses_js do
end

focus "does something", :with => 'additional metadata' do
end

focus "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

Overloads:

  • .focus(&example_implementation)

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .focus(doc_string, *metadata)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

  • .focus(doc_string, *metadata, &example_implementation)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 174

define_example_method :focus,    :focus => true

.for_filtered_examples(reporter, &block)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 655

def self.for_filtered_examples(reporter, &block)
  filtered_examples.each(&block)

  children.each do |child|
    reporter.example_group_started(child)
    child.for_filtered_examples(reporter, &block)
    reporter.example_group_finished(child)
  end
  false
end

.fspecify .fspecify(&example_implementation) .fspecify(doc_string, *metadata) .fspecify(doc_string, *metadata, &example_implementation)

Shortcut to define an example with ‘:focus => true`.

Examples:

fspecify do
end

fspecify "does something" do
end

fspecify "does something", :slow, :uses_js do
end

fspecify "does something", :with => 'additional metadata' do
end

fspecify "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

Overloads:

  • .fspecify(&example_implementation)

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .fspecify(doc_string, *metadata)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

  • .fspecify(doc_string, *metadata, &example_implementation)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 183

define_example_method :fspecify, :focus => true

.idString

Returns:

  • (String)

    the unique id of this example group. Pass this at the command line to re-run this exact example group.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 675

def self.id
  Metadata.id_from()
end

.idempotently_define_singleton_method(name, &definition)

This method is for internal use only.

Define a singleton method for the singleton class (remove the method if it’s already been defined).

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 40

def self.idempotently_define_singleton_method(name, &definition)
  (class << self; self; end).module_exec do
    remove_method(name) if method_defined?(name) && instance_method(name).owner == self
    define_method(name, &definition)
  end
end

.include_context(name, *args, &block)

Includes shared content mapped to ‘name` directly in the group in which it is declared, as opposed to it_behaves_like, which creates a nested group. If given a block, that block is also eval’d in the current context.

See Also:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 343

def self.include_context(name, *args, &block)
  find_and_eval_shared("context", name, caller.first, *args, &block)
end

.include_examples(name, *args, &block)

Includes shared content mapped to ‘name` directly in the group in which it is declared, as opposed to it_behaves_like, which creates a nested group. If given a block, that block is also eval’d in the current context.

See Also:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 353

def self.include_examples(name, *args, &block)
  find_and_eval_shared("examples", name, caller.first, *args, &block)
end

.initialize(inspect_output = nil)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 707

def initialize(inspect_output=nil)
  @__inspect_output = inspect_output || '(no description provided)'
  super() # no args get passed
end

.inspect

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 713

def inspect
  "#<#{self.class} #{@__inspect_output}>"
end

.it .it(&example_implementation) .it(doc_string, *metadata) .it(doc_string, *metadata, &example_implementation)

Defines an example within a group. This is the primary API to define a code example.

Examples:

it do
end

it "does something" do
end

it "does something", :slow, :uses_js do
end

it "does something", :with => 'additional metadata' do
end

it "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

Overloads:

  • .it(&example_implementation)

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .it(doc_string, *metadata)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

  • .it(doc_string, *metadata, &example_implementation)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 161

define_example_method :it

.metadata

The [Metadata](Metadata) object associated with this group.

See Also:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 51

def self.
  @metadata ||= nil
end

.method_missing(name, *args) (private)

See additional method definition at line 742.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 758

def self.method_missing(name, *args)
  if method_defined?(name)
    raise WrongScopeError,
          "`#{name}` is not available on an example group (e.g. a " \
          "`describe` or `context` block). It is only available from " \
          "within individual examples (e.g. `it` blocks) or from " \
          "constructs that run in the scope of an example (e.g. " \
          "`before`, `let`, etc)."
  end

  super
end

.next_runnable_index_for(file)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 485

def self.next_runnable_index_for(file)
  if self == ExampleGroup
    # We add 1 so the ids start at 1 instead of 0. This is
    # necessary for this branch (but not for the other one)
    # because we register examples and groups with the
    # `children` and `examples` collection BEFORE this
    # method is called as part of metadata hash creation,
    # but the example group is recorded with
    # `RSpec.world.example_group_counts_by_spec_file` AFTER
    # the metadata hash is created and the group is returned
    # to the caller.
    RSpec.world.num_example_groups_defined_in(file) + 1
  else
    children.count + examples.count
  end
end

.ordering_strategy

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 625

def self.ordering_strategy
  order = .fetch(:order, :global)
  registry = RSpec.configuration.ordering_registry

  registry.fetch(order) do
    warn <<-WARNING.gsub(/^ +\|/, '')
      |WARNING: Ignoring unknown ordering specified using `:order => #{order.inspect}` metadata.
      |         Falling back to configured global ordering.
      |         Unrecognized ordering specified at: #{location}
    WARNING

    registry.fetch(:global)
  end
end

.parent_groups

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 508

def self.parent_groups
  @parent_groups ||= ancestors.select { |a| a < RSpec::Core::ExampleGroup }
end

.pending .pending(&example_implementation) .pending(doc_string, *metadata) .pending(doc_string, *metadata, &example_implementation)

Shortcut to define an example with ‘:pending => true`

Examples:

pending do
end

pending "does something" do
end

pending "does something", :slow, :uses_js do
end

pending "does something", :with => 'additional metadata' do
end

pending "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

Overloads:

  • .pending(&example_implementation)

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .pending(doc_string, *metadata)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

  • .pending(doc_string, *metadata, &example_implementation)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 198

define_example_method :pending,  :pending => true

.remove_example(example)

Removes an example from the example group

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 373

def self.remove_example(example)
  reset_memoized
  examples.delete example
end

.reset_memoized

This method is for internal use only.

Clear memoized values when adding/removing examples

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 359

def self.reset_memoized
  @descendant_filtered_examples = nil
  @_descendants = nil
  @parent_groups = nil
  @declaration_locations = nil
end

.run(reporter = RSpec::Core::NullReporter)

Runs all the examples in this group.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 599

def self.run(reporter=RSpec::Core::NullReporter)
  return if RSpec.world.wants_to_quit
  reporter.example_group_started(self)

  should_run_context_hooks = descendant_filtered_examples.any?
  begin
    RSpec.current_scope = :before_context_hook
    run_before_context_hooks(new('before(:context) hook')) if should_run_context_hooks
    result_for_this_group = run_examples(reporter)
    results_for_descendants = ordering_strategy.order(children).map { |child| child.run(reporter) }.all?
    result_for_this_group && results_for_descendants
  rescue Pending::SkipDeclaredInExample => ex
    for_filtered_examples(reporter) { |example| example.skip_with_exception(reporter, ex) }
    true
  rescue Support::AllExceptionsExceptOnesWeMustNotRescue => ex
    for_filtered_examples(reporter) { |example| example.fail_with_exception(reporter, ex) }
    RSpec.world.wants_to_quit = true if reporter.fail_fast_limit_met?
    false
  ensure
    RSpec.current_scope = :after_context_hook
    run_after_context_hooks(new('after(:context) hook')) if should_run_context_hooks
    reporter.example_group_finished(self)
  end
end

.run_after_context_hooks(example_group_instance)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 585

def self.run_after_context_hooks(example_group_instance)
  set_ivars(example_group_instance, before_context_ivars)

  @currently_executing_a_context_hook = true

  ContextHookMemoized::After.isolate_for_context_hook(example_group_instance) do
    hooks.run(:after, :context, example_group_instance)
  end
ensure
  before_context_ivars.clear
  @currently_executing_a_context_hook = false
end

.run_before_context_hooks(example_group_instance)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 547

def self.run_before_context_hooks(example_group_instance)
  set_ivars(example_group_instance, superclass_before_context_ivars)

  @currently_executing_a_context_hook = true

  ContextHookMemoized::Before.isolate_for_context_hook(example_group_instance) do
    hooks.run(:before, :context, example_group_instance)
  end
ensure
  store_before_context_ivars(example_group_instance)
  @currently_executing_a_context_hook = false
end

.run_examples(reporter)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 641

def self.run_examples(reporter)
  ordering_strategy.order(filtered_examples).map do |example|
    next if RSpec.world.wants_to_quit
    instance = new(example.inspect_output)
    set_ivars(instance, before_context_ivars)
    succeeded = example.run(instance, reporter)
    if !succeeded && reporter.fail_fast_limit_met?
      RSpec.world.wants_to_quit = true
    end
    succeeded
  end.all?
end

.set_it_up(description, args, registration_collection, &example_group_block)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 410

def self.set_it_up(description, args, registration_collection, &example_group_block)
  # Ruby 1.9 has a bug that can lead to infinite recursion and a
  # SystemStackError if you include a module in a superclass after
  # including it in a subclass: https://gist.github.com/845896
  # To prevent this, we must include any modules in
  # RSpec::Core::ExampleGroup before users create example groups and have
  # a chance to include the same module in a subclass of
  # RSpec::Core::ExampleGroup. So we need to configure example groups
  # here.
  ensure_example_groups_are_configured

  # Register the example with the group before creating the metadata hash.
  # This is necessary since creating the metadata hash triggers
  # `when_first_matching_example_defined` callbacks, in which users can
  # load RSpec support code which defines hooks. For that to work, the
  # examples and example groups must be registered at the time the
  # support code is called or be defined afterwards.
  # Begin defined beforehand but registered afterwards causes hooks to
  # not be applied where they should.
  registration_collection << self

  @user_metadata = Metadata.build_hash_from(args)

  @metadata = Metadata::ExampleGroupHash.create(
    , @user_metadata,
    superclass.method(:next_runnable_index_for),
    description, *args, &example_group_block
  )

  config = RSpec.configuration
  config.(@metadata)

  ExampleGroups.assign_const(self)

  @currently_executing_a_context_hook = false

  config.configure_group(self)
end

.set_ivars(instance, ivars)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 685

def self.set_ivars(instance, ivars)
  ivars.each { |name, value| instance.instance_variable_set(name, value) }
end

.singleton_class

This method is for internal use only.

:nocov:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 720

def singleton_class
  class << self; self; end
end

.skip .skip(&example_implementation) .skip(doc_string, *metadata) .skip(doc_string, *metadata, &example_implementation)

Shortcut to define an example with ‘:skip => true`

Examples:

skip do
end

skip "does something" do
end

skip "does something", :slow, :uses_js do
end

skip "does something", :with => 'additional metadata' do
end

skip "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

Overloads:

  • .skip(&example_implementation)

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .skip(doc_string, *metadata)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

  • .skip(doc_string, *metadata, &example_implementation)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 195

define_example_method :skip,     :skip => true

.specify .specify(&example_implementation) .specify(doc_string, *metadata) .specify(doc_string, *metadata, &example_implementation)

Defines an example within a group. Useful for when your docstring does not read well off of ‘it`.

Examples:

RSpec.describe MyClass do
  specify "#do_something is deprecated" do
    # ...
  end
end
specify do
end

specify "does something" do
end

specify "does something", :slow, :uses_js do
end

specify "does something", :with => 'additional metadata' do
end

specify "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

Overloads:

  • .specify(&example_implementation)

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .specify(doc_string, *metadata)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

  • .specify(doc_string, *metadata, &example_implementation)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 170

define_example_method :specify

.store_before_context_ivars(example_group_instance)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 534

def self.store_before_context_ivars(example_group_instance)
  each_instance_variable_for_example(example_group_instance) do |ivar|
    before_context_ivars[ivar] = example_group_instance.instance_variable_get(ivar)
  end
end

.subclass(parent, description, args, registration_collection, &example_group_block)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 395

def self.subclass(parent, description, args, registration_collection, &example_group_block)
  subclass = Class.new(parent)
  subclass.set_it_up(description, args, registration_collection, &example_group_block)
  subclass.module_exec(&example_group_block) if example_group_block

  # The LetDefinitions module must be included _after_ other modules
  # to ensure that it takes precedence when there are name collisions.
  # Thus, we delay including it until after the example group block
  # has been eval'd.
  MemoizedHelpers.define_helpers_on(subclass)

  subclass
end

.superclass_before_context_ivars

This method is for internal use only.

:nocov:

See additional method definition at line 562.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 568

def self.superclass_before_context_ivars
  superclass.before_context_ivars
end

.superclass_metadataMetadata

This method is for internal use only.

Returns:

  • (Metadata)

    belonging to the parent of a nested ExampleGroup

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 71

def self.
  @superclass_metadata ||= superclass.respond_to?(:) ? superclass. : nil
end

.top_level_description

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 680

def self.top_level_description
  parent_groups.last.description
end

.traverse_tree_until(&block)

This method is for internal use only.

Traverses the tree of groups, starting with ‘self`, then the children, recursively. Halts the traversal of a branch of the tree as soon as the passed block returns true. Note that siblings groups and their sub-trees will continue to be explored. This is intended to make it easy to find the top-most group that satisfies some condition.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 476

def self.traverse_tree_until(&block)
  return if yield self

  children.each do |child|
    child.traverse_tree_until(&block)
  end
end

.update_inherited_metadata(updates)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 727

def self.(updates)
  .update(updates) do |key, existing_group_value, new_inherited_value|
    @user_metadata.key?(key) ? existing_group_value : new_inherited_value
  end

  RSpec.configuration.configure_group(self)
  examples.each { |ex| ex.(updates) }
  children.each { |group| group.(updates) }
end

.with_replaced_metadata(meta)

This method is for internal use only.

Temporarily replace the provided metadata. Intended primarily to allow an example group’s singleton class to return the metadata of the example that it exists for. This is necessary for shared example group inclusion to work properly with singleton example groups.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 61

def self.(meta)
   = 
  @metadata = meta
  yield
ensure
  @metadata = 
end

.xexample .xexample(&example_implementation) .xexample(doc_string, *metadata) .xexample(doc_string, *metadata, &example_implementation)

Shortcut to define an example with ‘:skip => ’Temporarily skipped with xexample’‘.

Examples:

xexample do
end

xexample "does something" do
end

xexample "does something", :slow, :uses_js do
end

xexample "does something", :with => 'additional metadata' do
end

xexample "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

Overloads:

  • .xexample(&example_implementation)

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .xexample(doc_string, *metadata)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

  • .xexample(doc_string, *metadata, &example_implementation)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 186

define_example_method :xexample, :skip => 'Temporarily skipped with xexample'

.xit .xit(&example_implementation) .xit(doc_string, *metadata) .xit(doc_string, *metadata, &example_implementation)

Shortcut to define an example with ‘:skip => ’Temporarily skipped with xit’‘.

Examples:

xit do
end

xit "does something" do
end

xit "does something", :slow, :uses_js do
end

xit "does something", :with => 'additional metadata' do
end

xit "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

Overloads:

  • .xit(&example_implementation)

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .xit(doc_string, *metadata)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

  • .xit(doc_string, *metadata, &example_implementation)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 189

define_example_method :xit,      :skip => 'Temporarily skipped with xit'

.xspecify .xspecify(&example_implementation) .xspecify(doc_string, *metadata) .xspecify(doc_string, *metadata, &example_implementation)

Shortcut to define an example with ‘:skip => ’Temporarily skipped with xspecify’‘.

Examples:

xspecify do
end

xspecify "does something" do
end

xspecify "does something", :slow, :uses_js do
end

xspecify "does something", :with => 'additional metadata' do
end

xspecify "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

Overloads:

  • .xspecify(&example_implementation)

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .xspecify(doc_string, *metadata)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

  • .xspecify(doc_string, *metadata, &example_implementation)

    Parameters:

    • doc_string (String)

      The example’s doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the example. Symbols will be transformed into hash entries with ‘true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 192

define_example_method :xspecify, :skip => 'Temporarily skipped with xspecify'

Instance Method Details

#described_class

Returns the class or module passed to the ‘describe` method (or alias). Returns nil if the subject is not a class or module.

Examples:

RSpec.describe Thing do
  it "does something" do
    described_class == Thing
  end
end
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_group.rb', line 99

def described_class
  self.class.described_class
end