【问题标题】:JBehave - how to write idempotent story?JBehave - 如何写幂等故事?
【发布时间】:2013-02-13 18:08:19
【问题描述】:

我想为我在 JBehave 中的验收测试编写一个“常见”故事,该故事仅在用户不存在时添加。 “createUser”故事本身就是一个验收测试用例。我想用GivenStories 来引用它,例如关于“modifyUser”、“deleteUser”等 - 但当“createUser”故事想要添加一个已经存在的用户时,我不想收到错误。

如何确保“createUser”仅在需要时执行?我在 JBehave 中找不到“条件故事”的任何解决方案。我也不能将“createUser”故事写成幂等的。

【问题讨论】:

    标签: junit jbehave


    【解决方案1】:

    这不是 JBehave 本身的问题 - 它不会在此级别上处理。我猜你想要这样的场景:

    Given user "John" exists with ID 1234
    When delete user request for ID 1234 was processed
    Then no user entry for ID 1234 should exist
    

    只有在给定步骤的绑定代码中,您才会检查并在必要时创建用户(执行您的条件插入操作)。这是在您的 Java 代码中处理的,而不是在验收测试级别。它看起来像这样:

    @Given("Given user \"$userName\" exists with ID $userId")
    public void givenUserExistsWithId(String userName, int userId)
    {
        if (!persistence.existsUser(userId))
        {
            persistence.createUser(userId, userName);
        }
    }
    

    这样场景是干净的,并且代码确保在执行给定状态后达到预期状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2011-09-07
      • 1970-01-01
      • 2019-12-31
      相关资源
      最近更新 更多