Tidycode

Tidy and clean code

Archive for June 2011

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