2015-12-22 40 views
2

如何在behat中的一个场景中的步骤之间使用变量? 我需要存储$ output的值,然后在第二步中使用它。Behat:在场景中的步骤之间使用变量

比方说,我有以下结构:

class testContext extends DefaultContext 
{ 
    /** @When /^I click "([^"]*)"$/ */ 
    public function iClick($element) { 
     if ($element = 2){ 
      $output = 5   
     } 
    } 


    /** @When /^I press "([^"]*)"$/ */ 
    public function iPress($button) { 
     if($button == $output){ 
     echo "ok"; 
     } 
    } 
} 

回答

3

上下文类可以是有状态的;场景的所有步骤都将使用相同的上下文实例。这意味着您可以使用常规类属性在步骤之间反转状态:

class testContext extends DefaultContext 
{ 
    private $output = NULL; 

    /** @When /^I click "([^"]*)"$/ */ 
    public function iClick($element) 
    { 
     if ($element = 2) { 
      $this->output = 5; 
     } 
    } 


    /** @When /^I press "([^"]*)"$/ */ 
    public function iPress($button) 
    { 
     if ($this->output === NULL) { 
      throw new BadMethodCallException("output must be initialized first"); 
     } 

     if ($button == $this->output) { 
      echo "ok"; 
     } 
    } 
} 
+0

非常感谢! 你帮了我 – laechoppe

相关问题