gpt4 book ai didi

unit-testing - 在 AAA 单元测试语法中混合使用 Assert 和 Act

转载 作者:行者123 更新时间:2023-12-04 23:09:39 28 4
gpt4 key购买 nike

可以混合 Assert 和 Act 步骤吗? 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 语法/样式的“合法”使用吗?

最佳答案

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);
}

关于unit-testing - 在 AAA 单元测试语法中混合使用 Assert 和 Act,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3770684/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com