Method: RSpec::Core::ExampleGroup.context

Defined in:
lib/rspec/core/example_group.rb

.contextRSpec::Core::ExampleGroup .context(&example_group_definition) ⇒ RSpec::Core::ExampleGroup .context(doc_string, *metadata, &example_implementation) ⇒ RSpec::Core::ExampleGroup

An alias of example_group. Generally used when grouping examples contextually (e.g. "with xyz", "when xyz" or "if xyz"). Generates a subclass of this example group which inherits everything except the examples themselves.

Examples:


RSpec.describe "something" do # << This describe method is defined in
                              # << RSpec::Core::DSL, included in the
                              # << global namespace (optional)
  before do
    do_something_before
  end

  before(:example, :clean_env) do
    env.clear!
  end

  let(:thing) { Thing.new }

  context "attribute (of something)" do
    # examples in the group get the before hook
    # declared above, and can access `thing`
  end

  context "needs additional setup", :clean_env, :implementation => JSON do
    # specifies that hooks with matching metadata
    # should be be run additionally
  end
end

Overloads:

  • .context(&example_group_definition) ⇒ RSpec::Core::ExampleGroup

    Parameters:

    • example_group_definition (Block)

      The definition of the example group.

  • .context(doc_string, *metadata, &example_implementation) ⇒ RSpec::Core::ExampleGroup

    Parameters:

    • doc_string (String)

      The group's doc string.

    • metadata (Array<Symbol>, Hash)

      Metadata for the group. Symbols will be transformed into hash entries with true values.

    • example_group_definition (Block)

      The definition of the example group.

Returns:

See Also:



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

define_example_group_method :context