【问题标题】:How to fix 'no prototype' when using php & behat使用 php & behat 时如何修复“无原型”
【发布时间】:2018-06-12 08:27:26
【问题描述】:

美好的一天。我正在使用 Behat 学习一些 PHP BDD 测试。我一直在密切关注这个Behat 3 教程:http://docs.behat.org/en/latest/quick_start.html

我在 Windows 10 上,使用 php 5.6.33。我已将composer.pharcomposer.json 添加到我的项目文件夹中,后一个文件如下所示:

{
    "require-dev": {
        "behat/behat": "~3.0.4",
        "phpunit/phpunit": "~4.1.0"
    }
}

然后,按照教程的建议,我按以下顺序运行命令:

  1. php composer.phar install
  2. vendor/bin/behat --init
  3. 我已将sample.feature 文件放入features 文件夹中。

功能文件如下所示:

Feature: Product basket
  In order to buy products
  As a customer
  I need to be able to put interesting products into a basket

  Rules:
  - VAT is 20%
  - Delivery for basket under £10 is £3
  - Delivery for basket over £10 is £2

  Scenario: Buying a single product under £10
    Given there is a "Sith Lord Lightsaber", which costs £5
    When I add the "Sith Lord Lightsaber" to the basket
    Then I should have 1 product in the basket
    And the overall basket price should be £9

  Scenario: Buying a single product over £10
    Given there is a "Sith Lord Lightsaber", which costs £15
    When I add the "Sith Lord Lightsaber" to the basket
    Then I should have 1 product in the basket
    And the overall basket price should be £20

  Scenario: Buying two products over £10
    Given there is a "Sith Lord Lightsaber", which costs £10
    And there is a "Jedi Lightsaber", which costs £5
    When I add the "Sith Lord Lightsaber" to the basket
    And I add the "Jedi Lightsaber" to the basket
    Then I should have 2 products in the basket
    And the overall basket price should be £20

但是当我运行 vendor/bin/behat --init 时,我得到了这个异常,它实际上并没有阻止生成代码 sn-ps:

    ReflectionException: Method FeatureContext::__construct does not have a prototype in D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Behat\Context\Reader\AnnotatedContextReader.php on line 86

    Call Stack:
        0.0054     237328   1. {main}() D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\bin\behat:0
        0.5684    2249712   2. Symfony\Component\Console\Application->run(???, ???) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\bin\behat:31
        0.7328    2785200   3. Behat\Testwork\Cli\Application->doRun(class Symfony\Component\Console\Input\ArgvInput, class Symfony\Component\Console\Output\ConsoleOutput) D:\Projects\php-projects\local-behat-phpunit-2\vendor\symfony\console\Application.php:117
        5.8172   12067888   4. Symfony\Component\Console\Application->doRun(class Symfony\Component\Console\Input\ArgvInput, class Symfony\Component\Console\Output\ConsoleOutput) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Cli\Application.php:102
        5.8180   12068848   5. Symfony\Component\Console\Application->doRunCommand(class Behat\Testwork\Cli\Command, class Symfony\Component\Console\Input\ArgvInput, class Symfony\Component\Console\Output\ConsoleOutput) D:\Projects\php-projects\local-behat-phpunit-2\vendor\symfony\console\Application.php:193
        5.8181   12069424   6. Symfony\Component\Console\Command\Command->run(class Symfony\Component\Console\Input\ArgvInput, class Symfony\Component\Console\Output\ConsoleOutput) D:\Projects\php-projects\local-behat-phpunit-2\vendor\symfony\console\Application.php:843
        5.8252   12079976   7. Behat\Testwork\Cli\Command->execute(class Symfony\Component\Console\Input\ArgvInput, class Symfony\Component\Console\Output\ConsoleOutput) D:\Projects\php-projects\local-behat-phpunit-2\vendor\symfony\console\Command\Command.php:241
        5.8558   12157688   8. Behat\Testwork\Tester\Cli\ExerciseController->execute(class Symfony\Component\Console\Input\ArgvInput, class Symfony\Component\Console\Output\ConsoleOutput) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Cli\Command.php:63
        5.9129   12222752   9. Behat\Testwork\Tester\Cli\ExerciseController->testSpecifications(class Symfony\Component\Console\Input\ArgvInput, array(2)) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Tester\Cli\ExerciseController.php:108
        5.9802   12264200  10. Behat\Testwork\EventDispatcher\Tester\EventDispatchingExercise->test(array(2), bool) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Tester\Cli\ExerciseController.php:146
        5.9802   12264432  11. Behat\Testwork\Tester\Runtime\RuntimeExercise->test(array(2), bool) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\EventDispatcher\Tester\EventDispatchingExercise.php:70
        6.0493   12324584  12. Behat\Testwork\EventDispatcher\Tester\EventDispatchingSuiteTester->setUp(class Behat\Behat\Context\Environment\UninitializedContextEnvironment, class Behat\Testwork\Specification\GroupedSpecificationIterator, bool) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Tester\Runtime\RuntimeExercise.php:69
        6.0691   12342664  13. Behat\Testwork\Hook\Tester\HookableSuiteTester->setUp(class Behat\Behat\Context\Environment\UninitializedContextEnvironment, class Behat\Testwork\Specification\GroupedSpecificationIterator, bool) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\EventDispatcher\Tester\EventDispatchingSuiteTester.php:59
        6.0976   12359008  14. Behat\Testwork\Hook\HookDispatcher->dispatchScopeHooks(class Behat\Testwork\Hook\Scope\BeforeSuiteScope) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Hook\Tester\HookableSuiteTester.php:63
        6.0976   12359192  15. Behat\Testwork\Hook\HookRepository->getScopeHooks(class Behat\Testwork\Hook\Scope\BeforeSuiteScope) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Hook\HookDispatcher.php:57
        6.0976   12359280  16. Behat\Testwork\Hook\HookRepository->getEnvironmentHooks(class Behat\Behat\Context\Environment\UninitializedContextEnvironment) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Hook\HookRepository.php:67
        6.0977   12359352  17. Behat\Testwork\Environment\EnvironmentManager->readEnvironmentCallees(class Behat\Behat\Context\Environment\UninitializedContextEnvironment) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Hook\HookRepository.php:50
        6.0977   12359600  18. Behat\Behat\Context\Environment\Reader\ContextEnvironmentReader->readEnvironmentCallees(class Behat\Behat\Context\Environment\UninitializedContextEnvironment) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Environment\EnvironmentManager.php:115
        6.0978   12360208  19. Behat\Behat\Context\Environment\Reader\ContextEnvironmentReader->readContextCallees(class Behat\Behat\Context\Environment\UninitializedContextEnvironment, string(14)) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Behat\Context\Environment\Reader\ContextEnvironmentReader.php:66
        6.1051   12363528  20. Behat\Behat\Context\Reader\ContextReaderCachedPerContext->readContextCallees(class Behat\Behat\Context\Environment\UninitializedContextEnvironment, string(14)) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Behat\Context\Environment\Reader\ContextEnvironmentReader.php:87
        6.1052   12363616  21. Behat\Behat\Context\Reader\AnnotatedContextReader->readContextCallees(class Behat\Behat\Context\Environment\UninitializedContextEnvironment, string(14)) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Behat\Context\Reader\ContextReaderCachedPerContext.php:52
        6.1052   12365152  22. Behat\Behat\Context\Reader\AnnotatedContextReader->readMethodCallees(string(14), class ReflectionMethod) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Behat\Context\Reader\AnnotatedContextReader.php:64
        6.1053   12365328  23. ReflectionMethod->getPrototype() D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Behat\Context\Reader\AnnotatedContextReader.php:86

我刚开始学习PHP,但据我了解接口AnnotatedContextReader确实没有__construct方法的原型,但提到接口扩展Context接口,这个有这样的方法原型.因此,我决定暂时跳过该异常并完成提供的教程。我可以运行我的行为测试,但控制台充满了ReflectionExceptions,抱怨我的测试步骤没有原型。这大大增加了执行时间,绝对不应该发生。关于我做错了什么的任何想法?代码 sn-ps 请参考我提供的教程链接。

【问题讨论】:

  • 你能粘贴你的 behat.yml 配置文件吗?
  • 使用最新的php并设置FeatureContext implements Context
  • @gvf 我已经按照帖子中提到的教程进行操作,他们不建议创建一个。据我了解,如果没有提供 behat.yml 文件,behat 会使用一些默认值吗?另外,我现在正在 Ubuntu 上使用 php7 进行尝试。升级对我来说不是一个选择,但也许我会抓住这个异常的根源。
  • 您能发布您的功能上下文文件和位置吗?
  • @lauda,我已经在我的bootstrap.php 文件中使用了FeatureContext implements Context, SnippetAcceptingContext。除非我可以同时拥有多个 php 版本,否则升级到 php7 对我来说不是一个选择。

标签: php bdd behat


【解决方案1】:

好的,所以这有点尴尬。经过一番挖掘,我发现这种行为与我正在使用的php 版本无关。显示异常跟踪是因为我启用了xdebug 扩展并将其配置为显示所有可能的输出。所以我相信这就是为什么我的控制台充满了ReflectionExceptions 并且测试执行非常缓慢的原因。设置此行解决了我的问题:

xdebug.show_exception_trace=0

另一方面,它并没有向我解释为什么首先会发生ReflectionException,它只是掩盖了异常跟踪。我知道这是因为基础接口中缺少方法原型,但这是故意的吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2020-02-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多