Tidy and clean code

Generics and Fluent Tests

leave a comment »

The recent changes in Fluent Tests was to introduce the usage of lambda expressions. With lambdas, we can add constraints when asserting collections.


However, a problem with generics arises. The Ensure class has two methods that is colliding

        public static EnsuredWrapper<T> That<T>(T compareValue)
return new EnsuredWrapper
<T>(assertionProvider, compareValue);

        public static CollectionEnsuredWrapper<IEnumerable<TElementType>, 
                              TElementType> That<TElementType>           
<TElementType> enumerable)
                              where TElementType : class
                                 return new
                                                            TElementType>(assertionProvider, enumerable);

These two methods, both called That, will compete and the latter will loose. That is because the generic expression in the first method is to greedy. Since there is no way to add a generic constraint that states that a generic parameter shouldn’t be of a specific type, there is no other way than to rename the method.

Another solution, and this is made in MSTest (the CollectionAssert class) , is to create a new Ensure class, CollectionEnsure that defines a correct That-method. So, which solution is prefered ?



The current implementation is using the first option with the ThatEnumerable-method.


Written by arvander

May 4, 2011 at 9:20 am

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: