Arrange Act Assert
Arrange/Act/Assert (AAA) is a pattern for arranging and formatting code in Unit Test methods.
It is a best practice to author your tests in more natural and convenient way. The idea is to develop a unit test by following these 3 simple steps:
- Arrange – setup the testing objects and prepare the prerequisites for your test.
- Act – perform the actual work of the test.
- Assert – verify the result.
Benefits of Using Arrange Act Assert
- Clearly separates what is being tested from the setup and verification steps.
- Clarifies and focuses attention on a historically successful and generally necessary set of test steps.
- Makes some test smells more obvious:
- Assertions intermixed with "Act" code.
- Test methods that try to test too many different things at once.
Arrange/Act/Assert with JustMock
Lets illustrate the benefits of the pattern with an example. We will use a sample warehouse and a dependent order object. The warehouse holds inventories of different products. An order contains a product and quantity.
The warehouse interface and the order class look like this:
Arrange
First we need an order:
Now let’s mock the warehouse:
We want to ensure that when an order of 2 cameras is placed, the warehouse returns that it has enough quantity of the product.
You can also check the Create Mocks By Example topic that demonstrates how you can arrange mock objects in more complex scenarios.
That’s it. We set up the testing objects for our test. Now let’s act.
Act
Fill our order from the warehouse.
Once we have executed the desired action, we need to ensure that it has been completed successfully and our order was actually filled, meaning that the warehouse really had inventory of 2 cameras.
Assert
We will use the Assert class from Microsoft.VisualStudio.TestTools.UnitTesting namespace (found in Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly – automatically added as a reference from Visual Studio while creating a Test Project) to ensure that the IsFilled
property of the order is set to true
.
With this simple example we illustrated the use of the AAA pattern and showed how easy it is to test your code with JustMock. Notice that you don’t need to change even a single line of your original code to set up, execute and verify its correctness.
Verify Interaction
Now let's take it a little further and verify not only the final result, but also the interaction while executing the test.
We arranged that when the warehouse’s HasInventory
method is called with specific parameters, it should return true
, but we never ensured that this method is actually called. Let's change the Arrange
method and mark that warehouse.HasInventory
must be called.
To verify this we need to call Mock.Assert
in the Assert
phase with the warehouse object.
Verify Order of Calls
Furthermore you may want to ensure that a set of method calls are executed in a particular order. Let`s assume we have the following IFoo
interface:
You use the Arrange
method to define the methods invocation order.
Again to verify this we need to call Mock.Assert
in the Assert
phase with the foo object.
Note that the InOrder
option also supports asserting the order of mock calls regardless of the instance within the test scope. Imagine that you have to validate that the user has logged in before using their shopping cart in your application.
Here we have defined the IUserValidationService
and the IShoppingCartService
services whose invocation order we are going to assert in the following test:
In the arrange phase we defined that the ValidateUser
call should be made only once and before the LoadCart
service call. The LoadCart
call should also occur only once and should follow the ValidateUser
service call. We act and then assert our expectations.
Refer to the Asserting Occurrence topic to learn more about asserting occurrence. The example also uses the Returns option in order to ignore the actual call and return a custom value.