04/03/2016 - BEHAT, SYMFONY
Eğer behat ile sub context dosyalar yaratıp, diğer context dosyaların kaynaklarına ulaşmak isterseniz, aşağıdaki örneği kullanabilirsiniz. Herşeyi tek bir FeatureContext dosyasında tutmak yerine, farklı context classlar yaratıp ortak kaynakları kullanabiliriz.
namespace Application\FrontendBundle\Features\Context;
use Behat\MinkExtension\Context\MinkContext;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FeatureContext extends MinkContext implements ContainerAwareInterface
{
const SAY_HELLO = 'Hello';
private $container;
public function __construct()
{
$this->useContext('you_context', new YouContext());
$this->useContext('me_context', new MeContext());
}
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function sayHello()
{
return self::SAY_HELLO;
}
}
namespace Application\FrontendBundle\Features\Context;
use Behat\Behat\Context\BehatContext;
class YouContext extends BehatContext
{
/**
* @Then /^I can access YouContext step definition$/
*/
public function iCanAccessYouContextStepDefinition()
{
return true;
}
}
namespace Application\FrontendBundle\Features\Context;
use Behat\Behat\Context\BehatContext;
use Behat\Behat\Exception\BehaviorException;
class MeContext extends BehatContext
{
/**
* @Then /^I can access MeContext step definition$/
*/
public function iCanAccessMeContextStepDefinition()
{
return true;
}
/**
* @Then /^I can access FeatureContext resources from MeContext$/
*/
public function iCanAccessFeatureContextResourcesFromMeContext()
{
/** @var FeatureContext $mainContext */
$mainContext = $this->getMainContext();
if ($mainContext->sayHello() != $mainContext::SAY_HELLO) {
throw new BehaviorException('Cannot access FeatureContext resources.');
}
}
}
Feature: .......
Scenario: I can access step definitions of other context files
Given I can access YouContext step definition
And I can access MeContext step definition
And I can access FeatureContext resources from MeContext
$ bin/behat --profile=frontend
Feature: ....
Scenario: I can access step definitions of other context files
Given I can access YouContext step definition
And I can access MeContext step definition
And I can access FeatureContext resources from MeContext
1 scenarios (1 passed)
3 steps (3 passed)
0m0.304s