variables

Variables in specs

Because Behave specs are plain Raku, my and our variables work exactly as you'd expect. You don't need a special DSL to declare state, though let is usually the right tool when you want per-example memoization with automatic reset.

Top-level variables

Declared at the top of the spec file, visible to every describe and it:

use BDD::Behave;

my $top      = 'top-level';
my @numbers  = (1, 2, 3);
my %settings = (timeout => 5);

describe 'top-level access', {
  it 'reads scalars, arrays, hashes', {
    expect($top).to.be('top-level');
    expect(@numbers[2]).to.be(3);
    expect(%settings<timeout>).to.be(5);
  }
}

Variables inside describe / context

A my declared inside a describe is shared across that group's examples. Mutations carry over between examples in registration order:

describe 'shared describe-level state', {
  my $counter = 0;

  it 'a', { expect(++$counter).to.be(1); }
  it 'b', { expect(++$counter).to.be(2); }
  it 'c', { expect(++$counter).to.be(3); }
}

This is the opposite of let, which resets between examples. Reach for let when you want isolation. Use a plain my when you intentionally want shared state.

Shadowing in nested contexts

Inner my declarations shadow outer ones the same way they would in any Raku block:

describe 'shadowing', {
  my $value = 'outer';

  it 'sees outer', { expect($value).to.be('outer') }

  context 'inner', {
    my $value = 'inner';
    it 'sees inner', { expect($value).to.be('inner') }
  }

  it 'outer unchanged after nested context', {
    expect($value).to.be('outer');
  }
}

Variables inside it

Variables declared inside an it block are example-local and never leak:

describe 'it-local vars', {
  it 'first',  { my $x = 'first';  expect($x).to.be('first') }
  it 'second', { my $x = 'second'; expect($x).to.be('second') }
}

When to use let vs a plain variable

Use a plain myUse let
Shared mutable state across examplesFresh value per example
Cheap constantsExpensive setup that should run lazily
Counters, accumulatorsTest subjects, fixtures

let works alongside plain variables: you can mix both in the same describe.

Classes, roles, enums

Type declarations follow the same scoping rules. See Classes inside specs.

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.