shared-contexts

Shared Contexts

Shared contexts let you package up let definitions and hooks under a name, then mix them into any number of describe or context groups. They're useful when several specs need the same setup (fixtures, logging, an authenticated user) without copy-paste.

shared-context and include-context are exported from BDD::Behave.

Defining a shared context

shared-context takes a name and a block. The block can call any DSL helper that's legal inside a group: let, before-each, after-each, before-all, after-all, even other include-context calls.

use BDD::Behave;

shared-context 'with widgets', {
  let(:widget, { 'gadget' });
  let(:count,  { 3 });
};

A shared context on its own does nothing: it isn't run until a group includes it.

Including a shared context

Call include-context 'name' from inside a describe or context. The shared block runs in the including group's scope, so its let definitions and hooks become part of that group exactly as if you'd written them inline.

describe 'shared-context inclusion', {
  include-context 'with widgets';

  it 'pulls in lets from the shared context', {
    expect(:widget).to.be('gadget');
    expect(:count).to.be(3);
  }
}

Hooks contributed by a shared context

Hooks declared in a shared context fire around the including group's examples like any other hook in that group:

shared-context 'with logging', {
  my @log;
  before-each { @log = [] }
  let(:log,   { @log });
};

describe 'shared-context with hooks', {
  include-context 'with logging';

  it 'starts with an empty log per example', {
    expect($*LET-RUNTIME.value('log').elems).to.be(0);
  }
}

Because the hook lives in the including group, it inherits into nested context blocks the same way an inline hook would. See Hooks for the full ordering rules.

Shadowing

A let defined in the including group (or any inner group) shadows a let of the same name from a shared context:

shared-context 'with default name', {
  let(:name, { 'default' });
};

describe 'inner let shadows shared-context let', {
  include-context 'with default name';
  let(:name, { 'override' });

  it 'sees the inner value', {
    expect(:name).to.be('override');
  }
}

Parameterized shared contexts

The shared block can take positional parameters. Pass them after the name in include-context:

shared-context 'with prefix', -> $prefix {
  let(:greeting, { "$prefix, world" });
};

describe 'parameterized shared context', {
  include-context 'with prefix', 'hello';

  it 'forwards arguments to the shared block', {
    expect(:greeting).to.be('hello, world');
  }
}

Combining shared contexts

A single group can include any number of shared contexts, and nested groups can include their own on top of what they inherit:

describe 'multiple shared contexts in one group', {
  include-context 'with widgets';
  include-context 'with default name';

  it 'merges lets from both shared contexts', {
    expect(:widget).to.be('gadget');
    expect(:name).to.be('default');
  }
}

describe 'nested groups inherit shared-context contributions', {
  include-context 'with widgets';

  context 'inner context', {
    include-context 'with default name';

    it 'sees lets from outer and inner shared contexts', {
      expect(:widget).to.be('gadget');
      expect(:name).to.be('default');
    }
  }
}

Errors

  • Calling include-context with an unregistered name dies with Unknown shared context: '<name>'.

  • Calling include-context outside a describe or context block dies: shared contexts can only be mixed into a group.

BDD::Behave v0.9.4

Behavior driven development framework

Authors

  • Greg Donald

License

Artistic-2.0

Dependencies

Provides

  • BDD::Behave
  • BDD::Behave::Benchmark
  • BDD::Behave::Benchmark::Baseline
  • BDD::Behave::Benchmark::Format
  • BDD::Behave::Bisect
  • BDD::Behave::Colors
  • BDD::Behave::Configuration
  • BDD::Behave::Coverage
  • BDD::Behave::DSL
  • BDD::Behave::Diff
  • BDD::Behave::DocExtractor
  • BDD::Behave::DryRun
  • BDD::Behave::Expectation
  • BDD::Behave::Failure
  • BDD::Behave::FailureStore
  • BDD::Behave::Failures
  • BDD::Behave::Files
  • BDD::Behave::Formatter
  • BDD::Behave::Formatter::Documentation
  • BDD::Behave::Formatter::HTML
  • BDD::Behave::Formatter::JSON
  • BDD::Behave::Formatter::JUnit
  • BDD::Behave::Formatter::JsonEvents
  • BDD::Behave::Formatter::Progress
  • BDD::Behave::Formatter::Registry
  • BDD::Behave::Formatter::TAP
  • BDD::Behave::Formatter::Tree
  • BDD::Behave::LetRuntime
  • BDD::Behave::Matcher
  • BDD::Behave::Matcher::Async
  • BDD::Behave::Matcher::Boolean
  • BDD::Behave::Matcher::Change
  • BDD::Behave::Matcher::Collection
  • BDD::Behave::Matcher::Core
  • BDD::Behave::Matcher::Custom
  • BDD::Behave::Matcher::Exception
  • BDD::Behave::Matcher::Numeric
  • BDD::Behave::Matcher::String
  • BDD::Behave::Matcher::Type
  • BDD::Behave::Mock::Allow
  • BDD::Behave::Mock::ArgMatcher
  • BDD::Behave::Mock::Double
  • BDD::Behave::Mock::HaveReceived
  • BDD::Behave::Mock::Spy
  • BDD::Behave::Mock::Stub
  • BDD::Behave::Parallel
  • BDD::Behave::Parallel::Distribution
  • BDD::Behave::Parallel::EventStream
  • BDD::Behave::Parallel::Manifest
  • BDD::Behave::Parallel::Queue
  • BDD::Behave::Parallel::WorkerPool
  • BDD::Behave::Runner
  • BDD::Behave::SharedContexts
  • BDD::Behave::SharedExamples
  • BDD::Behave::Slang
  • BDD::Behave::SpecLoader
  • BDD::Behave::SpecRegistry
  • BDD::Behave::SpecTree
  • BDD::Behave::SpecTree::Core
  • BDD::Behave::SpecTree::Example
  • BDD::Behave::SpecTree::ExampleGroup
  • BDD::Behave::SpecTree::Suite
  • BDD::Behave::Time
  • BDD::Behave::TypeName
  • BDD::Behave::Version
  • BDD::Behave::Watch
  • BDD::Behave::Watch::Session
  • BDD::Behave::Watch::SmartSelector
  • BDD::Behave::Watch::UI
  • BDD::Behave::Watch::Watcher
  • BDD::Behave::Worker

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.