Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 689
gdoron is supporting Monica Avatar asked Feb 21 '23 23:02

gdoron is supporting Monica


2 Answers

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.

like image 110
Darin Dimitrov Avatar answered Mar 08 '23 02:03

Darin Dimitrov


It depends, rule of thumb - Act stage represents execution of business logic under the test. In your case it depends on whether extraction affects any business logic, if result[i] indexer is straightforward collection item accesor - it is not Act since you alreay extracted data into the result variable, otherwise - it would be Act.

like image 39
sll Avatar answered Mar 08 '23 02:03

sll