Tidycode

Tidy and clean code

Unit testing cookies

leave a comment »

A way to solve the problem regarding unit testing is to wrap the cookie in a class and expose it as an interface. The actual implementation is resolved by a DI-container that makes it easy to mock the actual cookie when performing unit tests.

    interface IDomainCookie
    {
       
int ValueToStoreInCookie { get; set; }
    }
 
    class DomainCookie : IDomainCookie
    {
       
private readonly HttpResponse
_response;
       
private readonly HttpRequest
_request;

       public DomainCookie(HttpResponse response, HttpRequestrequest) 
   { 
      _response = response; 
      _request = request; 
   }

       public int ValueToStoreInCookie 
   { 
      
get
{ …. } 
      
set{ …. } 
   } 

}

 
This is a prefered solution in many ways. First, you decouple the solution from the actual HttpCookie. Second, the way you access the parameters are more straight forward as you simply type cookie.ValueToStoreInCookie = 2.
 

When performing the actual unit test we can mock or fake the implementation of the cookie.

    class DomainCookieTest
    {
       
public void
GetValueFromCookie()
        {
           
var resolvedCookie = Resolve<IDomainCookie
>();
           
Ensure
.That(resolvedCookie.ValueToStoreInCookie).IsGreaterThan(0);
        }
       

    private T Resolve<T>()
    {
          
// Resolve the generic argument using a DI-container (Unity, Ninject etc)
    }
}

Advertisements

Written by arvander

April 27, 2011 at 7:09 pm

Posted in Unit testing

Fluent testing

leave a comment »

The Assert class in MSTest can sometimes be a little limited when trying to write clean code. The readability of the code is far from optimal

int expected = 4;
int actual = 5;
Assert.IsTrue(expected>actual);

By extracting the variables expected and actual the intent is somewhat more readable. What I’m really is missing is a method called IsGreaterThan that compares two values. However, that method will take two parameters and there is a chance that they can be confused. Less parameters is better.

Since the class Assert is using static methods, creating an extension method is not an option. Instead, the natural thing to do seems to be to create a new class, MyAssert and attach a bunch of validating methods on that class instead.

public class MyAssert
{
 public void IsGreaterThan(int expected, int actual)
 {
 Assert .IsTrue(expected > actual);<
 }
}

But,  this makes it only partially easier to understand the code. The intent is clear, I want to assert that one value is to be greater than another but we still have the issue of the two parameters that has to be lifted out in separate fields in order to increase the readability.
To make it really readable, I’m using a fluent language so that I can type something like this.

 Ensure.That(4).IsGreaterThan(3);

This way the intent is totally clear and the parameters can not be confused.

There are a lot of frameworks availible that solves the problem in a very similar way, check out Codeplex for more information.

Written by arvander

April 26, 2011 at 10:03 pm