2017-06-26 12 views
3

我希望在使用Codeception時能夠在Gherkin特性文件中使用PhpStorm的「Go To Declaration」功能(在Mac上爲Command + B)。然而,PhpStorm似乎並沒有揣摩出定義的步驟,並輸出這樣的警告:使用Codeception和黃瓜時PhpStorm中未定義的步驟參考

未定義一步參考:[...] Warning screenshot

當我使用貝哈特,PhpStorm理解步驟已定義。

步驟來重現

  1. mkdir codeception
  2. cd codeception
  3. composer require "codeception/codeception" --dev
  4. ./vendor/bin/codecept bootstrap
  5. ./vendor/bin/codecept generate:feature acceptance first
  6. 打開在PhpStorm項目目錄。
  7. 確保PhpStorm知道安裝Codeception:Codeception PhpStorm configuration
  8. 確保安裝了PhpStorm插件小黃瓜Codeception框架
  9. 添加一步到tests/acceptance/first.feature
  10. ./vendor/bin/codecept gherkin:snippets acceptance

這導致下面的代碼。 (包括並非一切 - 讓我知道,如果我需要添加任何東西。)

tests/acceptance/first.feature

Feature: first 
    In order to ... 
    As a ... 
    I need to ... 

    Scenario: try first 
    When I visit "/" 

tests/_support/AcceptanceTester.php

<?php 

/** 
* Inherited Methods 
* @method void wantToTest($text) 
* @method void wantTo($text) 
* @method void execute($callable) 
* @method void expectTo($prediction) 
* @method void expect($prediction) 
* @method void amGoingTo($argumentation) 
* @method void am($role) 
* @method void lookForwardTo($achieveValue) 
* @method void comment($description) 
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL) 
* 
* @SuppressWarnings(PHPMD) 
*/ 
class AcceptanceTester extends \Codeception\Actor 
{ 
    use _generated\AcceptanceTesterActions; 

    /** 
    * Define custom actions here 
    */ 

    /** 
    * @When I visit :arg1 
    */ 
    public function iVisit($arg1) 
    { 
     throw new \Codeception\Exception\Incomplete("Step `I visit :arg1` is not defined"); 
    } 
} 

然而,PhpStorm不知道去哪裏iVisit()是。我怎樣才能解決這個問題?

回答

2

目前PhpStorm似乎使用貝哈特Context接口,以確定哪些類定義爲小黃瓜步驟實現在.feature文件,因此解決方法有PhpStorm找到在codeception測試的步驟是在某處添加Behat\Behat\Context\Context界面在你的源代碼樹

/* Context.php */ 
namespace Behat\Behat\Context; 

interface Context { } 

,然後讓AcceptanceTester實現該接口(即空標記界面)

class AcceptanceTester extends \Codeception\Actor implements Context ... 
+0

太好了,非常感謝。我從來沒有想過(仍然不明白它爲什麼起作用)。 –

1

大廈關閉Roverwolf's答案。

將其添加到AcceptanceTester文件的頂部。

namespace Behat\Behat\Context { 
    interface Context { } 
} 

然後讓AcceptanceTester執行那個。像這樣包裝名稱空間是PHP測試中的一個常見技巧,用於僞造其他名稱空間中存在的方法。

namespace { 
    class AcceptanceTester extends \Codeception\Actor implements \Behat\Behat\Context\Context 
    } 
}