Behat无法识别一些小黄瓜行

时间:2020-05-11 00:24:35

标签: php behat

我正在尝试运行behat,我创建了一个方案Outline,但是跳过了一些带有参数的行,并且某些变量被错误地获取了。

这是.feature上编写的小黄瓜方案大纲,具有已分配的功能:

Scenario Outline: CreatePostUseCase service # tests/integration/BlogApp/Feature/BlogApp.feature:7
    Given an <userid>                         # IntegrationTests\BlogApp\Context\BlogAppContext::aUserId()
    And an <email>        // <- email row is skipped and no anEmail() function assigned
    And a <password>                          # IntegrationTests\BlogApp\Context\BlogAppContext::aTitle()   // <- why is assigning aTitle() in password?
    When creating and saving a User object    # IntegrationTests\BlogApp\Context\BlogAppContext::creatingAndSavingAUserObject()
    Given a <title>    // <- why is not assigning aTitle() here?
    And a <body>       // <- this one is also skipped
    When creating a Post object               # IntegrationTests\BlogApp\Context\BlogAppContext::creatingAPostObject()
    Given a <publish> param                   # IntegrationTests\BlogApp\Context\BlogAppContext::aPublish()
    And persist the Post   // <- this one is also skipped
    Then an event should be launched          # IntegrationTests\BlogApp\Context\BlogAppContext::anEventShouldBeLaunched()

这是上下文:

/**
 * @Given an :userid
 */
public function aUserId($userId)
{
    $this->userId = $userId;
}

/**
 * @And an :email
 */
public function anEmail($email)
{
    $this->email = new Email($email);
}

/**
 * @And a :password
 */
public function aPassword($password)
{
    $this->password = new Password($password);
}

/**
 * @When creating and saving a User object
 */
public function creatingAndSavingAUserObject()
{
    $this->user = new User($this->userId, $this->email, $this->password);
    $this->userRepository = new UserRepository();
    $this->userRepository->save($this->user);
}

/**
 * @Given a :title
 */
public function aTitle($title)
{
    $this->title = $title;
}

/**
 * @And a :body
 */
public function aBody($body)
{
    $this->body = $body;
}

/**
 * @When creating a Post object
 */
public function creatingAPostObject()
{
    $this->user = new Post($this->title, $this->body);
}

/**
 * @Given a :publish param
 */
public function aPublishParam($publish)
{
    $this->publish = $publish;
}

/**
 * @And persist the Post
 */
public function persistThePost()
{
    $this->postRepository = new PostRepository();
    $this->eventQueue = new EventQueue();
    $this->createPostUseCase = new CreatePostUseCase($this->postRepository, $this->userRepository, $this->eventQueue);
    $this->createPostUseCase->execute($this->user, $this->post, $this->publish);
}

/**
 * @Then an event should be launched
 */
public function anEventShouldBeLaunched()
{
    $lastEvent = $this->eventQueue->getLastEvent();
    $event = $lastEvent->getPost();
    PHPUnit_Framework_Assert::assertEquals($event->getTitle(), $this->event->getTitle());
}

这些是.feature中的示例

Examples:
  | userid   | email               | password    | title           | body                                                                               | publish |
  | 1        | test@email.com      | abcd1234    | foo bar         | lorem ipsum dolor sit amet textae lungum is dolorem fistrum et duodenum pecatorum  | NULL    |
  | 2        | fake@email.com      | password123 | baz geek        | lorem ipsum dolor sit amet textae lungum is dolorem fistrum et duodenum            | true    |

我的代码出了点问题,但是我看不到它有什么问题或哪里有问题。有人可以帮我知道为什么要跳过一些参数吗?

1 个答案:

答案 0 :(得分:2)

使它们与众不同的是Gherkin语法,在您的情况下:

@And a :password@Given a :title相同,并且与@And an :email相同,因为:password和其他仅是一些标签,它们不属于该步骤。

Behat所看到的是:a <parameter>

添加其他不同的词,例如:@Given a title :title@And a password :password@And an email :email

更好,我建议阅读有关BDD的最佳做法。避免使用仅用于设置参数的步骤,避免使用参数在数组中或以您需要的任何方式生成一组值。

相关问题