Search code examples
c#unit-testingarrange-act-assert

Extraction of data in unit test AAA pattern


In "AAA" pattern where extraction of the act data should be written?
In the Act or in the Assert section?

Consider this Unit Test, the extraction of the two persons, should it be in the Act as in the example or in the Assert? we would like to make a standard for all of our UT in the company.

[Test]
public void Test()
{
    // Arrange
    var p1 = new Person();
    var p2 = new Person();
    Session.Save(p1);
    Session.Save(p2);

    // Act
    var result = new PersonQuery().GetAll();
    var firstPerson = result[0];
    var secondPerson = result[1];

    // Assert
    Assert.AreEqual(p1.Id, firstPerson.Id);
    Assert.AreEqual(p2.Id, secondPerson.Id);        
}

(please ignore that in this simple test I can write Assert.AreEqual(p1.Id, result[0].Id);)
I know it's not a huge problem, but I still want to know how to do things the best.


Solution

  • This should happen in the Assert phase:

    [Test]
    public void Test()
    {
        // Arrange
        var p1 = new Person();
        var p2 = new Person();
        Session.Save(p1);
        Session.Save(p2);
    
        // Act
        var result = new PersonQuery().GetAll();
    
        // Assert
        var firstPerson = result[0];
        var secondPerson = result[1];
        Assert.AreEqual(p1.Id, firstPerson.Id);
        Assert.AreEqual(p2.Id, secondPerson.Id);        
    }
    

    The Act phase only involves invoking the method under test.