Using BeforeScenario Hooks with a ScenarioOutline

582 views
Skip to first unread message

Betamax

unread,
Oct 9, 2012, 11:36:08 AM10/9/12
to be...@googlegroups.com
I have the following BefroeScenario hook defined:

 /**
     * @BeforeScenario @memcache
     */
    public function cleanMemcache(ScenarioEvent $event)
    {
        $this->_cache->cleanUp();
    }

Of course, if the @memcache tag sits on a Scenario Outline rather than a normal Scenario, then I get the error:
Catchable Fatal Error: Argument 1 passed to FeatureContext::cleanDb() must be an instance of Behat\Behat\Event\ScenarioEvent, instance of Behat\Behat\Event\OutlineExampleEvent given 

What is the best way around this, can I get normal Scenario hooks to work with Scenario Outlnes, or alternatively, what is the ScenarioOutline equivalent of BeforeScenario, is there a @BeforeScenarioOutline

po_taka

unread,
Oct 10, 2012, 1:49:26 AM10/10/12
to be...@googlegroups.com

The @BeforeScenario hook executes not only before each scenario in each feature, but before each example row in the scenario outline. Yes, each scenario outline example row works almost the same as a usual scenario, except that it sends a different event: Behat\Behat\Event\OutlineExampleEvent.

@AfterScenario functions exactly the same way, except after each scenario in each feature.

So, the @BeforeScenario or @AfterScenario hook will receive either a Behat\Behat\Event\ScenarioEvent or Behat\Behat\Event\OutlineExampleEvent instance, depending on the situation. It’s your job to differentiate between them if needed.



http://docs.behat.org/guides/3.hooks.html#scenario-hooks

Betamax

unread,
Oct 11, 2012, 7:21:24 AM10/11/12
to be...@googlegroups.com
Thanks for the response. My mistake was specifying the path in the @AfterScenario function. Now, if all my hooks just have $event in them, then both Scenarios and Scenario Outlines can call them.



        /**
     * @AfterScenario @memcache
     */
    public function cleanMemcache($event)
    {
        $this->_cache->cleanUp();
Reply all
Reply to author
Forward
0 new messages