What is the AAA pattern in unit testing?
Answer
The AAA (Arrange-Act-Assert) pattern is the standard structure for unit tests, providing clarity and consistency. Arrange: set up the test preconditions — create objects, configure mocks, prepare test data. Act: execute the code being tested — call the method/function under test. Assert: verify the outcome — check return value, verify state change, verify mock interactions. Example: // Arrange var mockRepo = new Mock<IOrderRepo>(); var service = new OrderService(mockRepo.Object); var order = new Order { Amount = 100 }; // Act var result = service.ProcessOrder(order); // Assert Assert.True(result.IsSuccess); mockRepo.Verify(r => r.Save(order), Times.Once);. Benefits: tests are self-documenting, easy to read, and consistently structured. Each test should have exactly one Act and one concept being tested. Also known as Given-When-Then (from BDD): Given (Arrange) → When (Act) → Then (Assert).