Tidycode

Tidy and clean code

Archive for the ‘Uncategorized’ Category

Conditions added to Fluent Tests

leave a comment »

When writing unit tests for a controller, you need to cast the result to the appropriate class, normally to a ViewResult.

            var customerController = new CustomerController();
           
var
result = customerController.Details(3);

            Ensure.That(result).Is.Not.Null();
           
Ensure.That(result).Is.InstanceOfType<ViewResult
>();
           
var viewResult = result as ViewResult
;
           
Ensure.That(viewResult.Model).Is.InstanceOfType<Customer
>();
           
var customer = viewResult.Model as Customer
;
           
Ensure.That(customer.FirstName).Is.EqualTo("Charlie");

With the new conditions in Fluent Tests you’ll cast the object automatically when asserting.

            var customerController = new CustomerController();
           
var
result = customerController.Details(3);

            Ensure.That(result).Is.InstanceOfType<ViewResult>()
                    .And(a => a.Model).Is.InstanceOfType<
Customer
>()
                    .And(a => a.FirstName).Is.EqualTo(
"Charlie");

This way you get to write the unit test clear and without all the noise you get when declaring variables and doing casts.

Advertisements

Written by arvander

June 20, 2011 at 9:26 pm

Posted in Uncategorized

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>           
                              (
IEnumerable
<TElementType> enumerable)
                              where TElementType : class
                              {            
                                 return new
CollectionEnsuredWrapper<IEnumerable<TElementType>,
                                                            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 ?

Ensure.ThatEnumerable(list).Where(a=>a.EndsWith(“*”)).Has.ElementCount(5);

CollectionEnsure.That(list).Where(a=>a.EndsWith(“*”)).Has.ElementCount(5);

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

Language support added to Fluent Tests

leave a comment »

Language support has been added upon a request. By simply delegating new language specific methods to the existing extension methods, a new language was created.

That means that we now can write test fluently in either english or swedish.

English : Ensure.That(5).Is.GreaterThan(4)

Swedish : Försäkra.Att(5).Är.StörreÄn(4)

Good or bad? I wont decide but I’ll stick with the english version myself. One problem that arises though are what Are is translated into? Any swedish user that has a suggestion, please leave a comment!

Written by arvander

May 3, 2011 at 11:25 pm

Posted in Uncategorized

More functions added to Fluent Tests

leave a comment »

Fluent Tests is continually being extended with more functionality. The basic types of tests are now available and more are coming soon.

Written by arvander

May 2, 2011 at 11:09 pm

Posted in Uncategorized

FluentTests now on CodePlex

leave a comment »

As of today, the 29th of april 2011, the FluentTests project is now available on CodePlex (fluenttests.codeplex.com).

Written by arvander

April 29, 2011 at 10:22 pm

Posted in Uncategorized