首页 > 解决方案 > 如何将参数对象变量从 behat.yml 传递给 FeatureContext.php 构造函数?

问题描述

在我的 behat.yml 上下文中,我需要将参数对象变量传递给 FeatureContext.php 构造函数方法。

在我的 behat.yml 文件中,我无法实例化 FeatureContext.php 中构造函数所需的类的实例。

当我运行 Behat 测试时,它显示一个错误,说我传入了一个“字符串”但需要类 FourZeroFour 的实例

这是我的 behat.yml

local:
  suites:
    default:
      paths:
        # Set features to repo root so that .feature files belonging to contrib
        # modules, themes, and profiles can be discovered.
        features: /var/www/mywebsite
        bootstrap: /var/www/mywebsite/tests/behat/features/bootstrap
      contexts:
        - Drupal\FeatureContext: 
          fourZeroFour: FourZeroFour
        - Drupal\DrupalExtension\Context\DrupalContext
        - Drupal\DrupalExtension\Context\MinkContext
        - Drupal\DrupalExtension\Context\MessageContext
        - Drupal\DrupalExtension\Context\DrushContext
  extensions:

这是我在 FeatureContext.php 中的构造方法

/**
 * FeatureContext class defines custom step definitions for Behat.
 */
class FeatureContext extends PageObjectContext implements SnippetAcceptingContext {

  private $fourZeroFour;

  public function __construct(FourZeroFour $fourZeroFour) {
    $this->fourZeroFour = $fourZeroFour;
  }

标签: drupal-8behat

解决方案


我的建议是:

  • FeatureContext应该扩展MinkContext或从 Drupal 扩展的另一个上下文MinkContext(如果有)(可能MinkContext来自 drupal)。
  • yml文件应该MinkContext只加载一次,只添加MinkContext直接延伸到另一个类的类。例如:如果 FeatureContext 扩展了 MinkContext 或者另一个扩展了 MinkContext 的类,那么在 yml 中你只能添加FeatureContext
  • 对于页面对象使用注入,以便您可以在 ide 中受益于自动完成
  • 关于构造函数,这似乎是一个页面对象,您可以使用use

推荐阅读