yceruto/behat-extension

v1.0.1 2024-06-26 18:55 UTC

This package is auto-updated.

Last update: 2024-09-26 19:40:01 UTC


README

一组 Behat 扩展,用于编写更好的 Behat 测试。

异常扩展

此扩展提供了一种捕获由您的行为抛出的异常并在测试中检查它们的方法。

安装

composer require --dev yceruto/behat-extension

配置

在您的 behat.yml 中启用扩展

default:
    extensions:
        Yceruto\BehatExtension\Extension\ExceptionExtension: ~

使用

一旦启用扩展,您可以在任何场景标题中使用 (!) 标记来指示必须捕获此场景中抛出的所有异常。通过使用此包中包含的一些预定义的 Behat 步骤,您可以检查异常类和消息。

示例

Feature: Manage blog posts
  As a blog owner
  I want to manage my blog posts
  So I can keep my blog up to date

  Scenario: Edit a blog post with invalid date
    Given I get a blog post with id "1"
    And I set a published date "3024-01-01"
    Then an exception should be thrown with message "The published date must be in the past."

注意

请注意,(!) 标记是捕获异常的强制要求。如果您不使用它,异常将像平常一样被抛出。

这些预定义步骤可以在 ExceptionAssertionTrait 中找到,一旦将其添加到您的 Behat 上下文类中,就会被激活。或者,您也可以创建自定义的 Behat 步骤并直接使用 ExceptionAssertion 类。

这是一个实现先前功能的 FeatureContext 类的示例

class FeatureContext implements Context
{
    use ExceptionAssertionTrait;

    /**
     * @Given I get a blog post with id :id
     */
    public function iGetABlogPostWithId(int $id): void
    {
        // code that gets the blog post...
    }

    /**
     * @Given I set a published date :date
     */
    public function iSetAPublishedDate(string $date): void
    {
        // code that throws an exception...
    
        throw new DomainException('The published date must be in the past.');
    }
}

许可

本软件根据 MIT 许可证 发布