Module: RSpec::SleepingKingStudios::Concerns::SharedExampleGroup

Overview

Methods for creating reusable shared example groups and shared contexts in a module that can be mixed into multiple RSpec example groups.

Examples:

module MySharedExamples
  extend RSpec::SleepingKingStudios::Concerns::SharedExampleGroup

  shared_examples 'my examples' do
    # Define shared examples here.
  end # shared_examples
end # module

RSpec.describe MyObject do
  include MySharedExamples

  include_examples 'my examples'
end # describe

Instance Method Summary collapse

Instance Method Details

#alias_shared_examples(new_name, old_name) ⇒ Object Also known as: alias_shared_context

Aliases a defined shared example group, allowing it to be accessed using a new name. The example group must be defined in the current context using ‘shared_examples`. The aliases must be defined before including the module into an example group, or they will not be available in the example group.

Parameters:

  • new_name (String)

    The new name to alias the shared example group as.

  • old_name (String)

    The name under which the shared example group is currently defined.

Raises:

  • ArgumentError If the referenced shared example group does not exist.



37
38
39
40
41
42
43
44
# File 'lib/rspec/sleeping_king_studios/concerns/shared_example_group.rb', line 37

def alias_shared_examples new_name, old_name
  example_group = shared_example_groups[self][old_name]
  definition    = example_group_definition(example_group)

  raise ArgumentError.new(%{Could not find shared examples "#{old_name}"}) if definition.nil?

  self.shared_examples new_name, &definition
end

#included(other) ⇒ 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.

Hook to merge defined example groups when included in another module.



50
51
52
53
54
# File 'lib/rspec/sleeping_king_studios/concerns/shared_example_group.rb', line 50

def included other
  super

  merge_shared_example_groups other
end

#shared_examples(name, &block) ⇒ Object #shared_examples(name, metadata, &block) ⇒ Object Also known as: shared_context

Defines a shared example group within the context of the current module. Unlike a top-level example group defined using RSpec#shared_examples, these examples are not globally available, and must be mixed into an example group by including the module. The shared examples must be defined before including the module, or they will not be available in the example group.

Overloads:

  • #shared_examples(name, &block) ⇒ Object

    Parameters:

    • name (String)

      Identifer to use when looking up this shared group.

    • block

      Used to create the shared example group definition.

  • #shared_examples(name, metadata, &block) ⇒ Object

    Parameters:

    • name (String)

      Identifer to use when looking up this shared group.

    • metadata (Array<Symbol>, Hash)

      Metadata to attach to this group; any example group with matching metadata will automatically include this shared example group.

    • block

      Used to create the shared example group definition.



72
73
74
# File 'lib/rspec/sleeping_king_studios/concerns/shared_example_group.rb', line 72

def shared_examples name, *, &block
  RSpec.world.shared_example_group_registry.add(self, name, *, &block)
end