TDD: Arrange Alter Act Assert

by EvanJPalmer

Prostheta_03-128

Here’s a pattern I’ve been using for managing maintainability of my unit tests (something I’ve had trouble with).

[Test]
public void MakeReissuePayment_DoesNotUpdatesReissuedAttendees_WhenUnSuccessful()
{
// Arrange
SetUpForPayment();

// Alter
_paymentSuccess = false;

// Act
MakeReissuePayment();

// Assert
_attendeeReissueRepository.Verify(x => x.Save(It.IsAny<AttendeeReissued>(), _transaction.Object), Times.Never());
}

What I’m doing here is the usual AAA pattern, but adding an alter step after I’ve arranged the standard values to run though this type of test.

So I will arrange my values to make the test pass. So that is, set up mock expectations with some private members, then make the smallest change possible to make the test unique/useful (alter).

It is important to note that in the SetUp() method, that NUnit calls before each test is run, it is important to reset all your private members, so that you can ensure they haven’t been altered in subsequent tests.

I’ve based this pattern of the pattern Roy Osherove offers in his great book The Art Of Unit Testing. It’s not a replacement for Object Mothers, but I find they can be overkill for some tests.

I never write the “Alter” comment (I just think it), because I don’t want to freak out other developers who are reading my code (“What the hell is Evan doing?”), but I think this really helps me keep my tests nice and maintainable.

Advertisements