my6solutions

asp .net, the social web & other distractions

 

Running Apps


PayPal - The safer, easier way to pay online!

Disclaimer

I am in no way affiliated with Microsoft or Google. I am just another developer trying to make a difference. All opinions and observations are usually my own.

Test Driven Development using Moq

Today, I have decided to start writing unit tests and to try a test driven development. Basically, a test driven development is one where the developer starts specifying automated unit tests first before actually starting to code. These unit tests are usually based on code specifications and what code features should be available. For me, hopefully this will lessen the number of breakages from fixing bugs, especially in large projects.

The ASP .NET MVC model is designed in a way that writing unit tests should be relatively easy and painless. To make things easier writing these unit tests, there are a few libraries that help in the creation of fake objects or mocks. These libraries include Rhino, NUnit, and Moq to name a few. I chose Moq because it supports Linq and the syntax seems easy and straightforward enough.

For example, for my project Raya, to test routes, the following is all that is required

 

[TestMethod]

public void TestRoutes()

{

    RouteCollection routes = new RouteCollection();

    MvcApplication.RegisterRoutes(routes);

 

    TestHelpers.AssertRoute(routes, "~/profile/1", new {controller = "profile", action = "Index"});

}

 

and the TestHelpers extension class is from Phil Haack.

 

public static class TestHelpers

{

    public static void AssertRoute(RouteCollection routes, string url, object expectations)

    {

        var httpContextMock = new Mock<HttpContextBase>();

        httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath)

            .Returns(url);

 

        RouteData routeData = routes.GetRouteData(httpContextMock.Object);

        Assert.IsNotNull(routeData, "Should have found the route");

 

        foreach (PropertyValue property in GetProperties(expectations))

        {

            Assert.IsTrue(string.Equals(property.Value.ToString(),

                routeData.Values[property.Name].ToString(),

                StringComparison.OrdinalIgnoreCase)

                , string.Format("Expected '{0}', not '{1}' for '{2}'.",

                property.Value, routeData.Values[property.Name], property.Name));

        }

    }

 

    private static IEnumerable<PropertyValue> GetProperties(object o)

    {

        return from prop in TypeDescriptor.GetProperties(o).Cast<PropertyDescriptor>()

               let val = prop.GetValue(o)

               where val != null

               select new PropertyValue { Name = prop.Name, Value = val };

    }

 

    private sealed class PropertyValue

    {

        public string Name {get;set;}

        public object Value {get;set;}

    }

}

 

 

 

Bookmark and Share

Categories: ASP .NET | TDD
Permalink | Comments (0) | Post RSSRSS comment feed