Unit Test
The starting point for all unit tests is one of the UnitTest abstract base classes:

public abstract class UnitTest<TTarget> ...

public abstract class UnitTest<TTarget, TReturnType> ...


The former class is for use with methods that do not have a return value, while the latter requires the method under test to return the specified type.

Which class you choose has an impact on the interface that must be used for defining actions and assertions.

The UnitTest classes have a Run method that should be used for specifying the test:

[TestMethod]
public override void Run()
{
	GivenThat<CalculatorIsDefaultConstructed>();

	When<AddingTwoNumbers>(13, 45);

	Then<ResultShouldEqual>(58);
}


The GivenThat method requires a type argument which implements an IInitializerMarker interface. GivenThat returns a fluent interface, allowing you to chain preconditions together:

	GivenThat<CalculatorIsDefaultConstructed>()
		.And<SomeOtherInitializationCode>()
		.And<FurtherInitializationCode>();


The When method requires a type argument which matches the action specification. However, you can also pass in either an Action<TTarget> or a Func<TTarget, TReturnType>. Note that this will circumvent the English-language of the type argument style, but some actions are too simple to necessitate a new class definition.

The Then method requires a type argument which matches the assertion specifcation. This also returns a fluent interface, much like GivenThat:

	Then<ResultShouldEqual>(58)
		.And<SomeOtherPostCondition>()
		.And<FurtherPostCondition>();


Notice that parameters have been supplied for the When and Then. This is also possible for GivenThat calls. Any parameters can be passed in here as the methods take params object[] as their argument. These values will be passed to the constructor of the supplied type. Notice, however, that any type mismatches will not be caught at compile time. In fact, failing to supply the correct number of arguments will not be caught at compile time.

Last edited Aug 15, 2011 at 2:12 PM by garymcleanhall, version 3

Comments

No comments yet.