Skip to content

describe / context

describe and context define a group of related examples. They are aliases — pick whichever reads better in plain English. Groups can be nested arbitrarily.

Basic usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
use BDD::Behave;

describe 'String methods', {
  it 'uppercases', {
    expect('hi'.uc).to.be('HI');
  }

  it 'reverses', {
    expect('abc'.flip).to.be('cba');
  }
}

Nesting with context

context reads naturally for branching scenarios:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
describe 'User#full-name', {
  context 'when both names are present', {
    it 'joins them with a space', {
      my $u = User.new(:first<Ada>, :last<Lovelace>);
      expect($u.full-name).to.be('Ada Lovelace');
    }
  }

  context 'when only the first name is present', {
    it 'returns the first name alone', {
      my $u = User.new(:first<Ada>);
      expect($u.full-name).to.be('Ada');
    }
  }
}

Scoping

Plain Raku scoping rules apply inside a group block — my $foo = 'bar' declared inside a describe is visible to its examples and nested groups, and shadowing works as you'd expect.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
describe 'shadowing', {
  my $value = 'outer';

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

  context 'inner', {
    my $value = 'inner';

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