2011-04-14 95 views
16

任何人都在使用Zend Framework的Behat?有關如何使用兩者的任何示例?Zend Framework與Behat的集成BDD

+1

我認爲你可能是這方面的先驅者。我甚至沒有聽說過這個。這聽起來並且從網站看起來很有用。 – 2011-06-02 02:10:54

+0

你想要測試哪些應用程序的元素?全堆棧,用戶界面,API的?根據您的測試目標,有許多不同的方法。 – 2011-07-08 12:31:03

回答

12

我明白了。它適用於PHPUnitZend_Test,因此您可以使用所有那些漂亮的assertXYZ()方法。首先,請確保已安裝behat,並可在系統$PATH中使用。我做了以下內容:

sudo pear channel-discover pear.symfony.com 
sudo pear channel-discover pear.behat.org 
sudo pear install behat/behat 

現在,創建一個目錄結構如下所示:

features 
    application 
     ControllerTestCase.php 
    bootstrap 
     FeatureContext.php 
    homepage.feature 

features/application/ControllerTestCase.php類是一個典型的Zend_Test測試執行:

<?php 
require_once 'Zend/Application.php'; 
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php'; 

class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase { 

    public $application; 

    public function setUp() { 
     $this->application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH 
       . '/configs/application.ini'); 
     $this->bootstrap = array($this, 'appBootstrap'); 
     parent::setUp(); 
    } 

    public function appBootstrap(){ 
     $this->application->bootstrap(); 
    } 
} 

features/bootstrap/FeatureContext.php類Behat需要自行啓動:

<?php 

use Behat\Behat\Context\ClosuredContextInterface, 
    Behat\Behat\Context\TranslatedContextInterface, 
    Behat\Behat\Context\BehatContext, 
    Behat\Behat\Exception\PendingException; 
use Behat\Gherkin\Node\PyStringNode, 
    Behat\Gherkin\Node\TableNode; 

require_once 'PHPUnit/Autoload.php'; 
require_once 'PHPUnit/Framework/Assert/Functions.php'; 

define('APPLICATION_ENV', 'testing'); 
define('APPLICATION_PATH', dirname(__FILE__) . '/../path/to/your/zf/application'); 

set_include_path('.' . PATH_SEPARATOR . APPLICATION_PATH . '/../library' 
     . PATH_SEPARATOR . get_include_path()); 

require_once dirname(__FILE__) . '/../application/ControllerTestCase.php'; 

class FeatureContext extends BehatContext { 

    protected $app; 

    /** 
    * Initializes context. 
    * Every scenario gets it's own context object. 
    * 
    * @param array $parameters context parameters (set up via behat.yml) 
    */ 
    public function __construct(array $parameters) { 
     $this->app = new ControllerTestCase(); 
     $this->app->setUp(); 
    } 

    /** 
    * @When /^I load the URL "([^"]*)"$/ 
    */ 
    public function iLoadTheURL($url) { 
     $this->app->dispatch($url); 
    } 

    /** 
    * @Then /^the module should be "([^"]*)"$/ 
    */ 
    public function theModuleShouldBe($desiredModule) { 
     $this->app->assertModule($desiredModule); 
    } 

    /** 
    * @Given /^the controller should be "([^"]*)"$/ 
    */ 
    public function theControllerShouldBe($desiredController) { 
     $this->app->assertController($desiredController); 
    } 

    /** 
    * @Given /^the action should be "([^"]*)"$/ 
    */ 
    public function theActionShouldBe($desiredAction) { 
     $this->app->assertAction($desiredAction); 
    } 

    /** 
    * @Given /^the page should contain a "([^"]*)" tag that contains "([^"]*)"$/ 
    */ 
    public function thePageShouldContainATagThatContains($tag, $content) { 
     $this->app->assertQueryContentContains($tag, $content); 
    } 

    /** 
    * @Given /^the action should not redirect$/ 
    */ 
    public function theActionShouldNotRedirect() { 
     $this->app->assertNotRedirect(); 
    } 

} 

現在你可以寫的功能,如features/homepage.feature

Feature: Homepage 
    In order to know ZF works with Behat 
    I need to see that the page loads. 

Scenario: Check the homepage 
    Given I load the URL "/index" 
    Then the module should be "default" 
    And the controller should be "index" 
    And the action should be "index" 
    And the action should not redirect 
    And the page should contain a "title" tag that contains "My Nifty ZF App" 

運行測試,cd到包含features文件夾的目錄,然後鍵入behat

祝你好運!

+0

有沒有辦法在單獨的文件中有步驟定義?不是所有的人都在一個'FeatureContext'類中? – takeshin 2011-09-11 11:28:57

+0

這似乎不適用於我。 ZF引導程序必須多次調用,因爲我得到「常量已定義」錯誤 – Andrew 2012-01-13 21:48:03

0

我的場景總是停在第一步。我終於明白了,有一個死亡,或者在我的代碼中退出某處,導致完整性停止。所以請確保您的應用程序不包含任何死亡或退出。現在它工作正常。