One of the greatest challenges of writing good tests is to keep the tests short and readable. This can be achieved by composition - here is the way I use to structure AAA-style tests using a few base classes:
[Test]
public void ProductValidator_IllegalProductCode_Fails()
{
// Arrange
var context = new TestContextBuilder<ProductValidatorContext>()
.WithScenario(AnonymousProduct)
.WithScenario(ProductHasIllegalProductCode)
.BuildContext();
ProductValidator sut = context.CreateSubjectUnderTest();
// Act
bool res = sut.Validate();
// Assert
Assert.IsFalse(res);
}
The goal is to provide more readable tests and to encapsulate (and reuse) test setup code. This code gallery project contains the base classes for composing tests using this technique. Please read the
related blog post for more information.