Class: RSpec::Core::ExampleGroup

Inherits:
Object
  • Object
show all
Extended by:
Hooks, MemoizedHelpers::ClassMethods, SharedExampleGroup
Includes:
MemoizedHelpers, Pending
Defined in:
lib/rspec/core/example_group.rb

Overview

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

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.

Direct Known Subclasses

AnonymousExampleGroup

Constant Summary collapse

INSTANCE_VARIABLE_TO_IGNORE =
:@__inspect_output
WrongScopeError =

Raised when an RSpec API is called in the wrong scope, such as `before` being called from within an example rather than from within an example group block.

Class.new(NoMethodError)

Constants included from Pending

Pending::NOT_YET_IMPLEMENTED, Pending::NO_REASON_GIVEN

Metadata collapse

Defining Examples collapse

Defining Example Groups collapse

Including Shared Example Groups collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SharedExampleGroup

shared_examples

Methods included from MemoizedHelpers::ClassMethods

let, let!, subject, subject!

Methods included from Hooks

after, append_after, around, before, hooks, prepend_before

Methods included from Pending

mark_fixed!, mark_pending!, mark_skipped!, #pending, #skip

Methods included from MemoizedHelpers

#is_expected, #should, #should_not, #subject

Constructor Details

#initialize(inspect_output = nil) ⇒ ExampleGroup

Returns a new instance of ExampleGroup



689
690
691
692
# File 'lib/rspec/core/example_group.rb', line 689

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)



740
741
742
743
744
745
746
747
748
749
750
# File 'lib/rspec/core/example_group.rb', line 740

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

  super
end

Class Method Details

.add_example(example) ⇒ Object

Adds an example to the example group



356
357
358
359
# File 'lib/rspec/core/example_group.rb', line 356

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

.before_context_ivarsObject



514
515
516
# File 'lib/rspec/core/example_group.rb', line 514

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

.childrenObject



451
452
453
# File 'lib/rspec/core/example_group.rb', line 451

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

.currently_executing_a_context_hook?Boolean

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

Returns:

  • (Boolean)


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

def self.currently_executing_a_context_hook?
  @currently_executing_a_context_hook
end

.declaration_locationsObject



650
651
652
653
654
# File 'lib/rspec/core/example_group.rb', line 650

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 = {}) ⇒ Object

See Also:

  • DSL#describe


235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/rspec/core/example_group.rb', line 235

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 = {}) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rspec/core/example_group.rb', line 142

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") ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/rspec/core/example_group.rb', line 306

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) ⇒ Object



76
77
78
79
80
# File '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_examplesObject



445
446
447
448
# File 'lib/rspec/core/example_group.rb', line 445

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

.descendantsObject



488
489
490
# File 'lib/rspec/core/example_group.rb', line 488

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

.descriptionString

Returns the current example group description

Returns:

  • (String)

    the current example group description



85
86
87
88
# File '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) ⇒ Object



683
684
685
686
687
# File 'lib/rspec/core/example_group.rb', line 683

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_configuredObject



503
504
505
506
507
508
509
510
511
# File 'lib/rspec/core/example_group.rb', line 503

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

.exampleObject .example(&example_implementation) ⇒ Object .example(doc_string, *metadata_keys, metadata = {}) ⇒ Object .example(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

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) ⇒ Object

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .example(doc_string, *metadata_keys, metadata = {}) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

  • .example(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:



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

define_example_method :example

.examplesObject



435
436
437
# File 'lib/rspec/core/example_group.rb', line 435

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

.fexampleObject .fexample(&example_implementation) ⇒ Object .fexample(doc_string, *metadata_keys, metadata = {}) ⇒ Object .fexample(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

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) ⇒ Object

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .fexample(doc_string, *metadata_keys, metadata = {}) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

  • .fexample(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:



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

define_example_method :fexample, :focus => true

.filtered_examplesObject



440
441
442
# File 'lib/rspec/core/example_group.rb', line 440

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

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



368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/rspec/core/example_group.rb', line 368

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

.fitObject .fit(&example_implementation) ⇒ Object .fit(doc_string, *metadata_keys, metadata = {}) ⇒ Object .fit(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

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) ⇒ Object

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .fit(doc_string, *metadata_keys, metadata = {}) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

  • .fit(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:



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

define_example_method :fit,      :focus => true

.focusObject .focus(&example_implementation) ⇒ Object .focus(doc_string, *metadata_keys, metadata = {}) ⇒ Object .focus(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

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) ⇒ Object

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .focus(doc_string, *metadata_keys, metadata = {}) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

  • .focus(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:



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

define_example_method :focus,    :focus => true

.for_filtered_examples(reporter, &block) ⇒ Object



638
639
640
641
642
643
644
645
646
647
# File 'lib/rspec/core/example_group.rb', line 638

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

.fspecifyObject .fspecify(&example_implementation) ⇒ Object .fspecify(doc_string, *metadata_keys, metadata = {}) ⇒ Object .fspecify(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

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) ⇒ Object

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .fspecify(doc_string, *metadata_keys, metadata = {}) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

  • .fspecify(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:



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

define_example_method :fspecify, :focus => true

.idString

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

Returns:

  • (String)

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



658
659
660
# File 'lib/rspec/core/example_group.rb', line 658

def self.id
  Metadata.id_from()
end

.idempotently_define_singleton_method(name, &definition) ⇒ Object

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



40
41
42
43
44
45
# File '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) ⇒ Object

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:



332
333
334
# File 'lib/rspec/core/example_group.rb', line 332

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

.include_examples(name, *args, &block) ⇒ Object

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:



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

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

.itObject .it(&example_implementation) ⇒ Object .it(doc_string, *metadata_keys, metadata = {}) ⇒ Object .it(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

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) ⇒ Object

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .it(doc_string, *metadata_keys, metadata = {}) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

  • .it(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:



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

define_example_method :it

.metadataObject

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

See Also:



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

def self.
  @metadata ||= nil
end

.next_runnable_index_for(file) ⇒ Object



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/rspec/core/example_group.rb', line 470

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_strategyObject



608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'lib/rspec/core/example_group.rb', line 608

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_groupsObject



493
494
495
# File 'lib/rspec/core/example_group.rb', line 493

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

.pendingObject .pending(&example_implementation) ⇒ Object .pending(doc_string, *metadata_keys, metadata = {}) ⇒ Object .pending(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

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) ⇒ Object

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .pending(doc_string, *metadata_keys, metadata = {}) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

  • .pending(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:



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

define_example_method :pending,  :pending => true

.remove_example(example) ⇒ Object

Removes an example from the example group



362
363
364
365
# File 'lib/rspec/core/example_group.rb', line 362

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

.reset_memoizedObject

Clear memoized values when adding/removing examples



348
349
350
351
352
353
# File 'lib/rspec/core/example_group.rb', line 348

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

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

Runs all the examples in this group.



584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/rspec/core/example_group.rb', line 584

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
    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
    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) ⇒ Object



570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/rspec/core/example_group.rb', line 570

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) ⇒ Object



532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/rspec/core/example_group.rb', line 532

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) ⇒ Object



624
625
626
627
628
629
630
631
632
633
634
635
# File 'lib/rspec/core/example_group.rb', line 624

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) ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/rspec/core/example_group.rb', line 399

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
  )
  ExampleGroups.assign_const(self)

  @currently_executing_a_context_hook = false

  RSpec.configuration.configure_group(self)
end

.set_ivars(instance, ivars) ⇒ Object



668
669
670
# File 'lib/rspec/core/example_group.rb', line 668

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

.skipObject .skip(&example_implementation) ⇒ Object .skip(doc_string, *metadata_keys, metadata = {}) ⇒ Object .skip(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

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) ⇒ Object

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .skip(doc_string, *metadata_keys, metadata = {}) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

  • .skip(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:



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

define_example_method :skip,     :skip => true

.specifyObject .specify(&example_implementation) ⇒ Object .specify(doc_string, *metadata_keys, metadata = {}) ⇒ Object .specify(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

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) ⇒ Object

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .specify(doc_string, *metadata_keys, metadata = {}) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

  • .specify(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:



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

define_example_method :specify

.store_before_context_ivars(example_group_instance) ⇒ Object



519
520
521
522
523
# File 'lib/rspec/core/example_group.rb', line 519

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) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/rspec/core/example_group.rb', line 384

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_ivarsObject

:nocov:



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

def self.superclass_before_context_ivars
  superclass.before_context_ivars
end

.superclass_metadataMetadata

Returns belonging to the parent of a nested RSpec::Core::ExampleGroup

Returns:



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

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

.top_level?Boolean

Returns:

  • (Boolean)


498
499
500
# File 'lib/rspec/core/example_group.rb', line 498

def self.top_level?
  superclass == ExampleGroup
end

.top_level_descriptionObject



663
664
665
# File 'lib/rspec/core/example_group.rb', line 663

def self.top_level_description
  parent_groups.last.description
end

.traverse_tree_until(&block) ⇒ Object

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.



461
462
463
464
465
466
467
# File 'lib/rspec/core/example_group.rb', line 461

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) ⇒ Object



709
710
711
712
713
714
715
716
717
# File 'lib/rspec/core/example_group.rb', line 709

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) ⇒ Object

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.



61
62
63
64
65
66
67
# File 'lib/rspec/core/example_group.rb', line 61

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

.xexampleObject .xexample(&example_implementation) ⇒ Object .xexample(doc_string, *metadata_keys, metadata = {}) ⇒ Object .xexample(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

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) ⇒ Object

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .xexample(doc_string, *metadata_keys, metadata = {}) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

  • .xexample(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:



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

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

.xitObject .xit(&example_implementation) ⇒ Object .xit(doc_string, *metadata_keys, metadata = {}) ⇒ Object .xit(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

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) ⇒ Object

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .xit(doc_string, *metadata_keys, metadata = {}) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

  • .xit(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:



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

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

.xspecifyObject .xspecify(&example_implementation) ⇒ Object .xspecify(doc_string, *metadata_keys, metadata = {}) ⇒ Object .xspecify(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

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) ⇒ Object

    Parameters:

    • example_implementation (Block)

      The implementation of the example.

  • .xspecify(doc_string, *metadata_keys, metadata = {}) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

  • .xspecify(doc_string, *metadata_keys, metadata = {}, &example_implementation) ⇒ Object

    Parameters:

    • doc_string (String)

      The example's doc string.

    • metadata (Hash) (defaults to: {})

      Metadata for the example.

    • metadata_keys (Array<Symbol>)

      Metadata tags for the example. Will be transformed into hash entries with `true` values.

    • example_implementation (Block)

      The implementation of the example.

Yields:

See Also:



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

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

Instance Method Details

#described_classObject

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

Examples:

describe Thing do
  it "does something" do
    described_class == Thing
  end
end


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

def described_class
  self.class.described_class
end

#inspectObject



695
696
697
# File 'lib/rspec/core/example_group.rb', line 695

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

#singleton_classObject

:nocov:



702
703
704
# File 'lib/rspec/core/example_group.rb', line 702

def singleton_class
  class << self; self; end
end