Mix Confirmation and Action in AAA Unit Test Syntax

Can I mix Assert and Act steps? Is AAA more oriented than the 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); } 

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

Is it "legal" to use AAA syntax / style?

+3
source share
2 answers

AAA is a guide to make your device tests more readable. In the above example, I would say that you did not achieve this goal.

I think the following tests make the script 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); } 
+7
source

AAA is just a guide to make your device tests more readable. It is perfectly normal to deviate if you have every reason to do so. You used spaces and comments to separate the different stages of the code to some extent, which is good. In such cases, it may also be useful to add comments explaining the story you are testing.

+1
source

Source: https://habr.com/ru/post/886191/


All Articles