Should it be “Arrange-Assert-Act-Assert”?

后端 未结 14 2528
不思量自难忘°
不思量自难忘° 2020-11-30 19:53

Regarding the classic test pattern of Arrange-Act-Assert, I frequently find myself adding a counter-assertion that precedes Act. This way I know that the passing assertion

14条回答
  •  萌比男神i
    2020-11-30 20:38

    I am now doing this. A-A-A-A of a different kind

    Arrange - setup
    Act - what is being tested
    Assemble - what is optionally needed to perform the assert
    Assert - the actual assertions
    

    Example of an update test:

    Arrange: 
        New object as NewObject
        Set properties of NewObject
        Save the NewObject
        Read the object as ReadObject
    
    Act: 
        Change the ReadObject
        Save the ReadObject
    
    Assemble: 
        Read the object as ReadUpdated
    
    Assert: 
        Compare ReadUpdated with ReadObject properties
    

    The reason is so that the ACT does not contain the reading of the ReadUpdated is because it is not part of the act. The act is only changing and saving. So really, ARRANGE ReadUpdated for assertion, I am calling ASSEMBLE for assertion. This is to prevent confusing the ARRANGE section

    ASSERT should only contain assertions. That leaves ASSEMBLE between ACT and ASSERT which sets up the assert.

    Lastly, if you are failing in the Arrange, your tests are not correct because you should have other tests to prevent/find these trivial bugs. Because for the scenario i present, there should already be other tests which test READ and CREATE. If you create a "Guard Assertion", you may be breaking DRY and creating maintenance.

提交回复
热议问题