以AAA单元测试语法混合断言和代理 [英] Mixing Assert and Act in AAA unit testing syntax
问题描述
可以将断言"和行动"步骤混合在一起吗? AAA不仅是准则,还是准则?还是我错过了什么?
Is it OK to mix Assert and Act steps? Is AAA more of a guideline than a rule? Or am I missing something?
这是我的考试:
[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次方法.每次通话后,我想确保我们没有真正取消对话框.然后在第三个调用中,应取消该对话框.
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.
这是AAA语法/样式的合法"使用吗?
Is this "legal" use of AAA syntax/styling?
推荐答案
AAA是使单元测试更具可读性的指南.在您提供的示例中,我认为您尚未实现该目标.
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);
}
这篇关于以AAA单元测试语法混合断言和代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!