Mixing Assert and Act in AAA unit testing syntax

强颜欢笑 提交于 2019-12-10 16:00:56

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/3770684/mixing-assert-and-act-in-aaa-unit-testing-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!