2017-06-26 14 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 
    } 
}