Extract data into unit test AAA template

In the "AAA" template, where do I need to record activity data?
In the Law or in the Assert section?

Consider this Unit Test, the extraction of two persons, should it be in the Law, as in the example or in 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 this is not a huge problem, but I still want to know how to do things better.

+4
source share
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 includes only a call to the test method.

+7
source

It depends, a rule of thumb - the stage of an action is the execution of business logic in a test. In your case, it depends on whether extraction affects any business logic if result[i] indexer is a simple element of a collection of collection elements - it is not Act , since you extracted data into the result variable, otherwise it would be Act .

+1
source

Source: https://habr.com/ru/post/1389105/


All Articles