在 AAA 单元测试语法中混合 Assert 和 Act

发布于 2024-09-24 19:33:45 字数 1163 浏览 1 评论 0原文

混合断言和行动步骤可以吗? AAA 更像是一个指南而不是一个规则吗?或者我错过了什么?

这是我的测试:

[TestMethod]
public void CancelButtonSelected_DontCancelTwiceThenCancel_DialogCloses()
{
    // Arrange
    IAddAddressForm form = Substitute.For<IAddAddressForm>();
    // Indicate that when Show CancelMessage is called it 
    //  should return cancel twice (saying we want to cancel the cancel)
    //  then it should return ok
    form.ShowCancelMessage().Returns(DialogResult.Cancel, 
         DialogResult.Cancel, DialogResult.OK);

    AddAddressController controller = new AddAddressController(form);
    AddressItem item = TestHelper.CreateAddressBob();

    // Act
    EnterAddressInfo(form, controller, item);
    controller.CancelButtonSelected();
    Assert.IsTrue(form.DialogResult == DialogResult.None);

    controller.CancelButtonSelected();
    Assert.IsTrue(form.DialogResult == DialogResult.None);

    controller.CancelButtonSelected();

    // Assert
    Assert.IsTrue(form.DialogResult == DialogResult.Cancel);
}

所以我调用一个方法 3 次。每次通话后,我想确保我们没有真正取消对话。然后在第三次调用时,应该取消对话框。

这是 AAA 语法/样式的“合法”使用吗?

Is it OK to mix Assert and Act steps? Is AAA more of a guideline than a rule? Or am I missing something?

Here is my test:

[TestMethod]
public void CancelButtonSelected_DontCancelTwiceThenCancel_DialogCloses()
{
    // Arrange
    IAddAddressForm form = Substitute.For<IAddAddressForm>();
    // Indicate that when Show CancelMessage is called it 
    //  should return cancel twice (saying we want to cancel the cancel)
    //  then it should return ok
    form.ShowCancelMessage().Returns(DialogResult.Cancel, 
         DialogResult.Cancel, DialogResult.OK);

    AddAddressController controller = new AddAddressController(form);
    AddressItem item = TestHelper.CreateAddressBob();

    // Act
    EnterAddressInfo(form, controller, item);
    controller.CancelButtonSelected();
    Assert.IsTrue(form.DialogResult == DialogResult.None);

    controller.CancelButtonSelected();
    Assert.IsTrue(form.DialogResult == DialogResult.None);

    controller.CancelButtonSelected();

    // Assert
    Assert.IsTrue(form.DialogResult == DialogResult.Cancel);
}

So I call a method 3 times. After each call, I want to make sure that we did not really cancel the dialog. Then on the third call, the dialog should be canceled.

Is this "legal" use of AAA syntax/styling?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吾性傲以野 2024-10-01 19:33:45

AAA 是使单元测试更具可读性的指南。在您提供的示例中,我认为您尚未实现该目标。

我认为以下测试使您正在测试的场景更具可读性。

[TestMethod]
public void CancelButtonSelected_ShouldSetDialogResultToNone_WhenFirstCancelButtonIsSelected()
{
    // Arrange
    IAddAddressForm form = ArrangeFormForCancelButtonSelectedTests();
    AddAddressController controller = ArrangeControllerForCancelButtonSelectedTests();

    // Act
    controller.CancelButtonSelected();

    // Assert
    Assert.IsTrue(form.DialogResult == DialogResult.None);
}

[TestMethod]
public void CancelButtonSelected_ShouldSetDialogResultToNone_WhenSecondCancelButtonIsSelected()
{
    // Arrange
    IAddAddressForm form = ArrangeFormForCancelButtonSelectedTests();
    AddAddressController controller = ArrangeControllerForCancelButtonSelectedTests();

    // Act
    controller.CancelButtonSelected();
    controller.CancelButtonSelected();

    // Assert
    Assert.IsTrue(form.DialogResult == DialogResult.None);

}

[TestMethod]
public void CancelButtonSelected_ShouldSetDialogResultToCancel_WhenThirdCancelButtonIsSelected()
{
    // Arrange
    IAddAddressForm form = ArrangeFormForCancelButtonSelectedTests();
    AddAddressController controller = ArrangeControllerForCancelButtonSelectedTests();

    // Act
    controller.CancelButtonSelected();
    controller.CancelButtonSelected();
    controller.CancelButtonSelected();

    // Assert
    Assert.IsTrue(form.DialogResult == DialogResult.Cancel);
}

AAA is a guideline to make your unit tests more readable. In the example you provided I would argue you have not achieved that goal.

I think the following tests make the scenario you are testing more readable.

[TestMethod]
public void CancelButtonSelected_ShouldSetDialogResultToNone_WhenFirstCancelButtonIsSelected()
{
    // Arrange
    IAddAddressForm form = ArrangeFormForCancelButtonSelectedTests();
    AddAddressController controller = ArrangeControllerForCancelButtonSelectedTests();

    // Act
    controller.CancelButtonSelected();

    // Assert
    Assert.IsTrue(form.DialogResult == DialogResult.None);
}

[TestMethod]
public void CancelButtonSelected_ShouldSetDialogResultToNone_WhenSecondCancelButtonIsSelected()
{
    // Arrange
    IAddAddressForm form = ArrangeFormForCancelButtonSelectedTests();
    AddAddressController controller = ArrangeControllerForCancelButtonSelectedTests();

    // Act
    controller.CancelButtonSelected();
    controller.CancelButtonSelected();

    // Assert
    Assert.IsTrue(form.DialogResult == DialogResult.None);

}

[TestMethod]
public void CancelButtonSelected_ShouldSetDialogResultToCancel_WhenThirdCancelButtonIsSelected()
{
    // Arrange
    IAddAddressForm form = ArrangeFormForCancelButtonSelectedTests();
    AddAddressController controller = ArrangeControllerForCancelButtonSelectedTests();

    // Act
    controller.CancelButtonSelected();
    controller.CancelButtonSelected();
    controller.CancelButtonSelected();

    // Assert
    Assert.IsTrue(form.DialogResult == DialogResult.Cancel);
}
遗心遗梦遗幸福 2024-10-01 19:33:45

AAA 只是使您的单元测试更具可读性的指南。如果你有充分的理由的话,偏离是完全可以的。您使用空格和注释在某种程度上分隔了代码中的不同阶段,这很好。在这种情况下,添加注释来解释您正在测试的故事也可能会有所帮助。

AAA is just a guideline to make your unit tests more readable. It is perfectly OK to deviate if you have a good reason to do so. You used whitespaces and comments to separate the different phases in code to some extent, which is good. In such cases it may also be helpful to add comments explaining the story you are testing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文