Skip to content

Instantly share code, notes, and snippets.

@stof
Created December 29, 2014 08:44
Show Gist options
  • Save stof/930e968829cd66751a3a to your computer and use it in GitHub Desktop.
Save stof/930e968829cd66751a3a to your computer and use it in GitHub Desktop.
Accessing other contexts in Behat 3
default:
suites:
default:
contexts: [FeatureContext, SubContext]
<?php
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
class FeatureContext implements Context {
private $subContext;
/** @BeforeScenario */
public function gatherContexts(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();
$this->subContext = $environment->getContext('SubContext');
}
/** @When I set the state :state */
function IGetTheContext($state) {
$this->subContext->setState($state);
}
}
Feature: SubContext
Scenario: Access the sub context
When I set the state "foo"
Then the state should be "foo"
<?php
use Behat\Behat\Context\Context;
class SubContext implements Context {
private $state;
function setState($state) {
$this->state = $state;
}
/** @Then the state should be :state */
function theStateShouldBe($state) {
PHPUnit_Framework_Assert::assertSame($state, $this->state);
}
}
@mpdude
Copy link

mpdude commented Jan 21, 2020

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment