Question

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.

Was it helpful?

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top