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

rubocop:disable Metrics/ClassLength 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.

Constant Summary collapse

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)

Metadata collapse

Including Shared Example Groups collapse

Class Method Summary collapse

Methods included from Hooks

after, append_after, around, before, prepend_before

Methods included from MemoizedHelpers::ClassMethods

let, let!, subject, subject!

Methods included from SharedExampleGroup

shared_examples

Methods included from Pending

#pending, #skip

Methods included from MemoizedHelpers

#is_expected, #should, #should_not, #subject

Class Method Details

.add_example(example) ⇒ void

Adds an example to the example group



354
355
356
357
# File 'lib/rspec/core/example_group.rb', line 354

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

.currently_executing_a_context_hook?Boolean

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

Returns:

  • (Boolean)


525
526
527
# File 'lib/rspec/core/example_group.rb', line 525

def self.currently_executing_a_context_hook?
  @currently_executing_a_context_hook
end

.descriptionString

Returns the current example group description.

Returns:

  • (String)

    the current example group description



84
85
86
87
# File 'lib/rspec/core/example_group.rb', line 84

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

.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.



656
657
658
# File 'lib/rspec/core/example_group.rb', line 656

def self.id
  Metadata.id_from()
end

.include_context(name, *args, &block) ⇒ void

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:



330
331
332
# File 'lib/rspec/core/example_group.rb', line 330

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

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

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:



340
341
342
# File 'lib/rspec/core/example_group.rb', line 340

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

.initialize(inspect_output = nil) ⇒ void



687
688
689
690
# File 'lib/rspec/core/example_group.rb', line 687

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

.metadatavoid

The Metadata object associated with this group.

See Also:



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

def self.
  @metadata ||= nil
end

.remove_example(example) ⇒ void

Removes an example from the example group



360
361
362
363
# File 'lib/rspec/core/example_group.rb', line 360

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

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

Runs all the examples in this group.



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

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

Instance Method Details

#described_classvoid

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


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

def described_class
  self.class.described_class
end