# 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:

```C#
public delegate void ProductRemoveEventHandler(string productName, int quantity);

public interface IWarehouse
{
    event ProductRemoveEventHandler ProductRemoved;

    string Manager { get; set; }

    bool HasInventory(string productName, int quantity);
    void Remove(string productName, int quantity);
}

public class Order
{
    public Order(string productName, int quantity)
    {
        this.ProductName = productName;
        this.Quantity = quantity;
    }

    public string ProductName { get; private set; }
    public int Quantity { get; private set; }
    public bool IsFilled { get; private set; }

    public void Fill(IWarehouse warehouse)
    {
        if (warehouse.HasInventory(this.ProductName, this.Quantity))
        {
            warehouse.Remove(this.ProductName, this.Quantity);
        }
    }

    public virtual string Receipt(DateTime orderDate)
    {
        return string.Format("Ordered {0} {1} on {2}", this.Quantity, this.ProductName, orderDate.ToString("d"));
    }
}
```

```VB
Public Delegate Sub ProductRemovedEventHandler(productName As String, quantity As Integer)

Public Interface IWarehouse
    Event ProductRemoved As ProductRemovedEventHandler

    Property Manager() As String

    Function HasInventory(productName As String, quantity As Integer) As Boolean
    Sub Remove(productName As String, quantity As Integer)
End Interface

Public Class Order
    Public Sub New(productName As String, quantity As Integer)
        Me.ProductName = productName
        Me.Quantity = quantity
    End Sub

    Public Property ProductName() As String
        Get
            Return m_ProductName
        End Get
        Private Set(value As String)
            m_ProductName = value
        End Set
    End Property
    Private m_ProductName As String
    Public Property Quantity() As Integer
        Get
            Return m_Quantity
        End Get
        Private Set(value As Integer)
            m_Quantity = value
        End Set
    End Property
    Private m_Quantity As Integer
    Public Property IsFilled() As Boolean
        Get
            Return m_IsFilled
        End Get
        Private Set(value As Boolean)
            m_IsFilled = value
        End Set
    End Property
    Private m_IsFilled As Boolean

    Public Sub Fill(warehouse As IWarehouse)
        If warehouse.HasInventory(Me.ProductName, Me.Quantity) Then
            warehouse.Remove(Me.ProductName, Me.Quantity)
            IsFilled = True
        End If
    End Sub

    Public Overridable Function Receipt(orderDate As DateTime) As String
        Return String.Format("Ordered {0} {1} on {2}", Me.Quantity, Me.ProductName, orderDate.ToString("d"))
    End Function
End Class
```

## Arrange

First we need an order:

```C#
var order = new Order("Camera", 2);
```

```VB
Dim order = New Order("Camera", 2)
```

Now let’s mock the warehouse:

```C#
var warehouse = Mock.Create<IWarehouse>();
```

```VB
Dim warehouse = Mock.Create(Of IWarehouse)()
```

We want to ensure that when an order of 2 cameras is placed, the warehouse returns that it has enough quantity of the product.

```C#
Mock.Arrange(() => warehouse.HasInventory("Camera", 2)).Returns(true);
```

```VB
Mock.Arrange(Function() warehouse.HasInventory("Camera", 2)).Returns(true)
```

> note You can also check the [Create Mocks By Example](https://www.telerik.com/products/justmock/documentation/basic-usage/arrange-act-assert/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.

```C#
order.Fill(warehouse);
```

```VB
order.Fill(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`.

```C#
Assert.IsTrue(order.IsFilled);
```

```VB
Assert.IsTrue(order.IsFilled)
```

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

You can verify not only the final result, but also that specific methods were actually called during the test.

In the example above, `HasInventory` returns `true` but we never confirmed it was actually called. Update the arrangement to require it:

```C#
Mock.Arrange(() => warehouse.HasInventory("Camera", 2)).Returns(true).MustBeCalled();
```

```VB
Mock.Arrange(Function() warehouse.HasInventory("Camera", 2)).Returns(true).MustBeCalled()
```

To verify this we need to call `Mock.Assert` in the `Assert` phase with the warehouse object.

```C#
Mock.Assert(warehouse);
```

```VB
Mock.Assert(warehouse)
```

## Verify Order of Calls

Use `InOrder()` on each arranged call to define the expected execution order. Then call `Mock.Assert(mock)` in the assert phase to verify the order was respected. `InOrder()` tracks call order across all mock instances within the same test scope — not just within a single mock.

For example, assume you have the following `IFoo` interface:

```C#
public interface IFoo
{
    void Submit();
    void Echo();
}
```

```VB
Public Interface IFoo
    Sub Submit()
    Sub Echo()
End Interface
```

Arrange each call with InOrder() in the order you expect them to be executed:

```C#
[TestMethod]
public void ShouldVerifyCallsOrder()
{
    // Arrange
    var foo = Mock.Create<IFoo>();

    Mock.Arrange(() => foo.Submit()).InOrder();
    Mock.Arrange(() => foo.Echo()).InOrder();

    // Act
    foo.Submit();
    foo.Echo();

    // Assert
    Mock.Assert(foo);
}
```

```VB
<TestMethod()>
Public Sub ShouldVerifyCallsOrder()
    ' Arrange
    Dim foo = Mock.Create(Of IFoo)()

    Mock.Arrange(Sub() foo.Submit()).InOrder()
    Mock.Arrange(Sub() foo.Echo()).InOrder()

    ' Act
    foo.Submit()
    foo.Echo()

    ' Assert
    Mock.Assert(foo)
End Sub
```

Call `Mock.Assert` in the assert phase with the mock object to verify the call order.

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.

```C#
public interface IUserValidationService
{
    int ValidateUser(string userName, string password);
}

public interface IShoppingCartService
{
    IList<string> LoadCart(int userID);
}
```

```VB
Public Interface IUserValidationService
    Function ValidateUser(ByVal userName As String, ByVal password As String) As Integer
End Interface

Public Interface IShoppingCartService
    Function LoadCart(ByVal userID As Integer) As IList(Of String)
End Interface
```

Here we have defined the `IUserValidationService` and the `IShoppingCartService` services whose invocation order we are going to assert in the following test:

```C#
[TestMethod]
public void ShouldAssertInOrderForDifferentInstancesInTestMethodScope()
{
    string userName = "Bob";
    string password = "Password";
    int userID = 5;
    var cart = new List<string> {"Foo", "Bar"};

    // Arrange
    var userServiceMock = Mock.Create<IUserValidationService>();
    var shoppingCartServiceMock = Mock.Create<IShoppingCartService>();

    Mock.Arrange(() => userServiceMock.ValidateUser(userName, password)).Returns(userID).InOrder().OccursOnce();
    Mock.Arrange(() => shoppingCartServiceMock.LoadCart(userID)).Returns(cart).InOrder().OccursOnce();

    // Act
    userServiceMock.ValidateUser(userName, password);
    shoppingCartServiceMock.LoadCart(userID);

    // Assert
    Mock.Assert(userServiceMock);
    Mock.Assert(shoppingCartServiceMock);
}
```

```VB
<TestMethod()>
Public Sub ShouldAssertInOrderForDifferentInstancesInTestMethodScope()
    Dim userName As String = "Bob"
    Dim password As String = "Password"
    Dim userID As Integer = 5
    Dim cart As IList(Of String) = {"Foo", "Bar"}

    ' Arrange
    Dim userServiceMock = Mock.Create(Of IUserValidationService)()
    Dim shoppingCartServiceMock = Mock.Create(Of IShoppingCartService)()

    Mock.Arrange(Function() userServiceMock.ValidateUser(userName, password)).Returns(userID).InOrder().OccursOnce()
    Mock.Arrange(Function() shoppingCartServiceMock.LoadCart(userID)).Returns(cart).InOrder().OccursOnce()

    ' Act
    userServiceMock.ValidateUser(userName, password)
    shoppingCartServiceMock.LoadCart(userID)

    ' Assert
    Mock.Assert(userServiceMock)
    Mock.Assert(shoppingCartServiceMock)
End Sub
```

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](https://www.telerik.com/products/justmock/documentation/basic-usage/asserting-occurrence) topic to learn more about asserting occurrence. The example also uses the [Returns](https://www.telerik.com/products/justmock/documentation/basic-usage/mock/returns) option to return a custom value.

## See Also

* [JustMock API Basics](https://www.telerik.com/products/justmock/documentation/getting-started/api-basics/basics)
* [Create Mocks By Example](https://www.telerik.com/products/justmock/documentation/basic-usage/create-mocks-by-example)
* [Returns](https://www.telerik.com/products/justmock/documentation/basic-usage/mock/returns)
* [Throws](https://www.telerik.com/products/justmock/documentation/basic-usage/mock/throws)
* [DoInstead](https://www.telerik.com/products/justmock/documentation/basic-usage/mock/do-instead)
* [Matchers](https://www.telerik.com/products/justmock/documentation/basic-usage/matchers)
* [Asserting Occurrence](https://www.telerik.com/products/justmock/documentation/basic-usage/asserting-occurrence)
