This post is a summary of a lightningtalk I held for some of my colleagues at Objectware back in August 2008.
AAA is a way to organize your tests. The contents of the tests are divided in to three parts:
[TestMethod] public void GetCustomerById() { // Arrange const int CustomerId = 5; var repository = new CustomerRepository(); // Act & Assert Assert.AreEqual(CustomerId, repository.GetCustomerById(CustomerId)); }The same test written in AAA style:
[TestMethod] public void GetCustomerById() { // Arrange const int CustomerId = 5; var repository = new CustomerRepository(); // Act Customer customer = repository.GetCustomerById(CustomerId); // Assert Assert.AreEqual(CustomerId, customer); }The example above is simple, but the value of using AAA style becomes more apparent for complex tests.
In the following example, which parts of the code are arranging, acting and asserting?
The record/playback style should not be used, since it results in tests which are very hard to read and understand.The same test as above written in AAA style:
[TestMethod] public void ShouldGenerateCreditInvoiceDocumentWhenNegativeAmountNew() { // Arrange ICustomerService customerService = MockRepository.GenerateStub<ICustomerService>(); IDocumentService documentService = MockRepository.GenerateStub<IDocumentService>(); double amount = -1000; Customer customer = new Customer() { Id = 12345, Name = "Name1" }; customerService.Stub(stub => stub.GetCustomer(customer.Id)).Return(customer); InvoicingProcess invoicingProcess = new InvoicingProcess(customerService, documentService); // Act invoicingProcess.Invoice(customer.Id, amount); // Assert documentService.AssertWasCalled(stub => stub.CreateDocument(customer, DocumentType.CreditInvoice)); }
The following mocking frameworks supports AAA:
Etiketter: TDD