Inside testing we have the AAA sintax or pattern, in my case I prefer the AAA Pattern, this pattern has been accepted globaly as a standard to write Unit Tests.
This pattern divides the unit test in three sections.
- Arrange: This part initialize all the objects needed to complete the test and stablish the data values that we are going to use in the actual Test.
- Act: It calls the specific method to test depending on it has or not some parameters.
- Assert: This last part checks that the tested executed method retrieves a known answer or value, just as it has to be on a real case.
As all patterns, it doesn't have to be required in all the test or projects, but it's recommended to follow one of them as good practices.
There are some reasons to use this pattern but I'm going to talk about the first two.
- The first and most important is about the organization, it will allow you to share with other developers and they can understand clearly what they are doing.
- Writing with this pattern must be three separated comments AAA so this can clearly be helpful to you and others.
One thing that is very important on this pattern is that once completed the Arrange (Initialization) part, you must not touch those initializations, just focus on verify the results of the test, on the other hand if the method to test inside our Tests doesn't meet with AAA pattern requirements, we need to open a Warning, this one lets you to detect possible code smells on code.
In other aspects of this patterns each one of the programmed tests must be separated as modules this is to not depending on other methods.
After those important aspects let's talk on a Mock example on how to write one Test with AAA pattern.
As we can see on this example, I have used the AAA Pattern inside of my test, this separating the blocks of code in Arrange, Act and Assert
There you go !!!!
if you have some comments or suggestions please contact me and do not forget your like on this post.