The Developer 2/2013

Page 16

Consider this Structural Inspection test: [Fact]

Even if you have a test that you can properly divide into the three distinct AAA phases, you don’t need comments or formatting if it’s only three lines of code:

public void SutIsBasketElement() {

// Arrange // Act?

var sut = new Vat(); // Assert }

Assert.IsAssignableFrom<IBasketElement>(sut);

[Theory]

[InlineData("", "", 1, 1, 1, 1, true)]

[InlineData("foo", "", 1, 1, 1, 1, false)] [InlineData("", "bar", 1, 1, 1, 1, false)]

[InlineData("foo", "foo", 1, 1, 1, 1, true)]

[InlineData("foo", "foo", 2, 1, 1, 1, false)] [InlineData("foo", "foo", 2, 2, 1, 1, true)]

Notice the question mark after the // Act comment. It seems that the writer of the test was unsure if the act of creating an instance of the System Under Test (SUT) constitutes the Act phase.

[InlineData("foo", "foo", 2, 2, 2, 1, false)] [InlineData("foo", "foo", 2, 2, 2, 2, true)] public void EqualsReturnsCorrectResult( string sutName,

string otherName, int sutUnitPrice,

You could just as well argue that creating the SUT is part of the Arrange phase:

int otherUnitPrice, int sutQuantity,

int otherQuantity,

[Fact]

public void SutIsBasketElement() {

{

// Arrange

new BasketItem(otherName, otherUnitPrice, otherQuantity));

// Act

}

Assert.IsAssignableFrom<IBasketElement>(sut);

However, now the Act phase is empty. Clearly, using code comments to split two lines of code into three phases is not helpful to the reader. THREE LINES OF CODE AND LESS Here’s a simpler alternative: [Fact]

var sut = new BasketItem(sutName, sutUnitPrice, sutQuantity); var actual = sut.Equals(

var sut = new Vat(); // Assert

bool expected)

}

Assert.Equal(expected, actual);

Three lines of code, and three phases of AAA; I think it’s obvious what goes where – even if this single test method captures eight different test cases. SIMPLE TESTS WITH MORE THAN THREE LINES OF CODE When you have more than three lines of code, you’ll need to help the reader identify what goes where. As long as you can keep it simple, I think that you accomplish this best with simple whitespace:

public void SutIsBasketElement() {

}

var sut = new Vat();

Assert.IsAssignableFrom<IBasketElement>(sut);

[Fact]

public void UseBasketPipelineOnExpensiveBasket() {

var basket = new Basket(

new BasketItem("Chocolate", 50, 3),

When there’s only two lines of code, the test is so simple that you don’t need help from code comments. If you wanted, you could even reduce that test to a single line of code, by inlining the sut variable:

new BasketItem("Gruyère", 45.5m, 1), new BasketItem("Barolo", 250, 2));

CompositePipe<Basket> pipeline = new BasketPipeline();

[Fact]

var actual = pipeline.Pipe(basket);

{

var expected = new Basket(

public void SutIsBasketElement()

}

new BasketItem("Chocolate", 50, 3),

Assert.IsAssignableFrom<IBasketElement>(new Vat());

new BasketItem("Gruyère", 45.5m, 1), new BasketItem("Barolo", 250, 2), new Discount(34.775m),

Such a test is either a degenerate case of AAA where one or more phase is empty, or else it doesn’t really fit into the AAA pattern at all. In these cases, code comments are only in the way, so it’s better to omit them.

16

new Vat(165.18125m),

new BasketTotal(825.90625m));

}

Assert.Equal(expected, actual);


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.