Module: RSpec::Core::SharedExampleGroup

Included in:
ExampleGroup
Defined in:
lib/rspec/core/shared_example_group.rb

Overview

Shared example groups let you define common context and/or common examples that you wish to use in multiple example groups.

When defined, the shared group block is stored for later evaluation. It can later be included in an example group either explicitly (using include_examples, include_context or it_behaves_like) or implicitly (via matching metadata).

Named shared example groups are scoped based on where they are defined. Shared groups defined in an example group are available for inclusion in that example group or any child example groups, but not in any parent or sibling example groups. Shared example groups defined at the top level can be included from any example group.

Defined Under Namespace

Modules: TopLevelDSL

Instance Method Summary (collapse)

Instance Method Details

- (void) shared_examples(name, &block) - (void) shared_examples(name, metadata, &block) - (void) shared_examples(metadata, &block) Also known as: shared_context, shared_examples_for

Stores the block for later use. The block will be evaluated in the context of an example group via include_examples, include_context, or it_behaves_like.

Examples:

shared_examples "auditable" do
  it "stores an audit record on save!" do
    expect { auditable.save! }.to change(Audit, :count).by(1)
  end
end

describe Account do
  it_behaves_like "auditable" do
    let(:auditable) { Account.new }
  end
end

See Also:



54
55
56
57
58
59
60
61
62
63
# File 'lib/rspec/core/shared_example_group.rb', line 54

def shared_examples(name, *args, &block)
  top_level = self == ExampleGroup
  if top_level && RSpec.[:in_example_group]
    raise "Creating isolated shared examples from within a context is " \
          "not allowed. Remove `RSpec.` prefix or move this to a " \
          "top-level scope."
  end

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