JBehave - 如何写出幂等故事?

时间:2013-02-13 18:08:19

标签: junit jbehave

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

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

1 个答案:

答案 0 :(得分: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);
    }
}

这样情况就是干净的,代码确保在执行给定状态后的预期状态。

相关问题