Building a Simple Blog Engine with ASP.NET MVC and LINQ - Part 4
Published: 30 Apr 2008
In the fourth part of this series, Keyvan talks about the unit testing concepts related to the MVC pattern and how Microsoft has adapted these concepts for its ASP.NET MVC framework. He shows these principles and the process that should be followed to unit test an ASP.NET MVC application.
by Keyvan Nayyeri
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 732913/ 866


In the first three parts of this series I talked about some major aspects of MVC pattern, ASP.NET MVC Framework and KBlog (the simple blog engine that is being created in this series as a showcase). So far, I discussed controller and data model as two of the three main components in Model View Controller (MVC) pattern.

The main reason for the popularity and demand for MVC pattern among web developers (not only ASP.NET developers) is the fact that MVC enables testing capabilities for developers to easily test their web applications. So MVC brings Test-Driven Development (TDD) to web applications, which is a very common and popular way to develop software these days.

Of course, there are some ways to unit test your traditional web applications, but things get much easier with MVC pattern.

The topic of this part and the next part is unit testing. In MVC pattern you can test your application easily and just by testing your controller classes. Here I show you how to unit test your MVC applications, but before that we need to get started with some theoretical concepts in this part.

Before digging into the main body of this part, let me point out that the first three parts of this article series were written based on the first public version and CTP of ASP.NET MVC Framework while this part is written based on the second CTP that you can download from here.

A Short Overview of Unit Testing

Test-Driven Development (aka TDD) is nowadays the most common way to develop software among architects and developers. Raise of Agile development methodologies and the growing number of small software teams is the main reason to choose TDD as the main option for developing software (of course there are some other reasons as well).

I suppose that you (as readers of this article) have a background in TDD and unit testing from the past because there is no doubt that I cannot teach you such stuff here, but a short related introduction to the topic of this article is mandatory.

The main and most common part of TDD is unit testing. Unit testing is a process and a set of techniques to divide an application into smaller (small) units that are independent from each other and can work isolated then test each unit to make sure it works as expected.

Unit testing works based on some principles. The first one is breaking the application into small pieces which lets you examine their working easily. The second principle is the fact that these units are isolated and work independently from others. This isolation lets you change a unit without having any effect on another unit.

Unit testing techniques test these pieces to see if each piece works as expected for different situations and input data. In this case, once you pass all tests, you can move on to the next stage in your development. But the good point is the fact that if you change other elements and units of the code then they would not break your existing code and if they do (which is not surprisingly unexpected) then your existing tests will fail and alert you about a problem.

So you can go back and forth and refractor your code without breaking existing code and consistency of your code and this is the main reason that many developers have chosen this as their primary process for development.

However, I cannot talk much about Test-Driven Development and unit testing here and I should not, of course. But I just want to say that there are various techniques for unit testing data layer, abstract code or web services. There are also some well-documented patterns and practices on how to write your code to make the unit testing process easier.

Why MVC and Why ASP.NET MVC?

Web development is a common development scenario which requires its own techniques and patterns. Since web development in many server technologies is strongly correlated with some stuff, like web server properties or client request details, then software developers had to find a way to reduce these dependencies. Regarding the above introduction, you would know why?

The reason is the isolation. We have to reduce these dependencies to improve the isolation level. So what was the solution? The solution was the introduction of new development patterns including Model View Controller (MVC). The most famous technology that is designed for MVC pattern is Ruby on Rails and one of its amazing popularity reasons is from here.

This evolvement in web development technologies forced Microsoft to think about an adaption of this pattern for its ASP.NET technology and the result was the introduction of ASP.NET MVC framework.

So in a nutshell:

·         Unit testing is great (and in my opinion is a mandatory part of development for today's software)!

·         Unit testing requires a high level of abstraction and isolation between different units of the application.

·         Unit testing also requires that you reduce the dependencies between your built-in platform objects to be able to create and raise them independently.

·         Traditional ASP.NET web form applications had some strong dependencies between some objects like HttpContext, HttpRequest and HttpRequest.

·         Microsoft had to adapt MVC for its web development technology.

·         Microsoft had to reduce all those dependencies and improve the isolation level.

·         So Microsoft had to build ASP.NET MVC pattern with new considerations for isolation and abstraction.

So ASP.NET MVC framework comes with new designs for some main ASP.NET classes like HttpContext, HttpResponse or HttpRequest and of course, follows MVC pattern.

Abstraction and Isolation in ASP.NET MVC

We need a good level of abstraction and isolation, but how we can achieve it in our design and code? This is possible via abstract base classes and interfaces because both these concepts provide some level of abstraction for us.

Unit testing is tight to abstract base classes and interfaces and there are lots of patterns and practices around these. I do not want to step into this topic here.

When you use abstract base classes and interfaces for abstraction then you are able to test instances of these classes easily because they all have some main methods that you need to call and use. This is very helpful when unit testing because it gives you much control on the classes that play a role in your testing units.

But how does one choose between abstract base classes and interfaces? Even though they both provide abstraction for us, each of them has some limitations for its own.

Phil Haack, my dear friend and a member of ASP.NET MVC team at Microsoft, has some blog posts that explain these limitations and what they faced with usage of interfaces in first CTP of ASP.NET MVC framework that forced them to switch to abstract base classes in the second CTP and most likely all the future versions.

·         Versioning Issues With Abstract Base Classes and Interfaces

·         Abstract Base Classes Have Versioning Problems Too

·         The Cost Of Breaking Changes

From an end user point of view, you may not care much about such stuff though!

But finally Microsoft applied abstract base classes for abstraction purposes in the second public release of ASP.NET MVC.

You see that these changes help you to unit test an ASP.NET MVC application in the next sections of this article.

Microsoft has moved all these abstractions and classes to a new assembly called System.Web.Abstractions so you can traverse through the classes in this assembly to see what is there.

Figure 1 shows all these classes with their hierarchy and structure which can give you some thoughts about this abstraction level.

Figure 1

As you see, here there are seven pairs of base classes and some classes that derive from them. All these abstract base classes are representing some traditional ASP.NET classes that had many dependencies. So this abstraction can help the unit testing process as you will notice this later in the next part.

But in a nutshell, we can group these classes into seven categories:

·         HttpSessionState

·         HttpServerUtility

·         HttpContext

·         HttpResponse

·         HttpRequest

·         HttpBrowserCapabilities

·         HttpCachePolicy

As you see, all these categories are strongly tight to server, client properties and characteristics, but this new abstraction helps to reduce these dependencies and improve the testability.

Testing Process in MVC Pattern

But what is the testing process in MVC pattern for ASP.NET MVC? Model View Controller pattern is designed in a way to simplify the testing process by dividing the architecture of the application into independent components (Model, View and Controller). The philosophy behind MVC is to break the application in these components to limit the unit testing to Controller component.

In other words, MVC is designed in a way that you only need to unit test your controller classes and this is all the point.

Controller is a pure programming class and nothing more. On the other hand, it is completely independent from data model and views (data layer and user interface) and here you notice the point. This independence helps you to test your application just by testing the controller as a programming class.

I will show you how to unit test controllers in the next part.

Improved Testing Features in ASP.NET MVC Preview 2

The new version of ASP.NET MVC framework (Preview 2) comes with some new major features that improve the testing capabilities for developers. One of these major changes is the ability to create an integrated unit test project for your ASP.NET MVC projects when you create them. Prior to this, you had to create an ASP.NET MVC project then create a unit test project for your code manually. But now Visual Studio asks you to choose if you want to create a new Visual Studio unit test project for your code.

This is shown in Figure 2. After creating an ASP.NET MVC project this dialog shows up to ask you whether you want to create the test project or not.

Figure 2

Of course you can ignore this and not create a unit test project.

On the other hand, Visual Studio test framework (known as MsTest) is not the only choice for you. There are many professional developers who like and prefer other testing frameworks like NUnit (adaption of xUnit for .NET) or MbUnit that are both very well-known among .NET developers and you can use them.

Other Parts


The fourth part of this article series was all about unit testing concepts to enable Test-Driven Development in your ASP.NET MVC applications, which is the primary benefit behind MVC pattern.

I started with an introduction to the importance of unit testing in MVC pattern and then gave a quick overview of unit testing and its main goals then stepped into the process that led to build ASP.NET MVC framework. After that I talked about the necessity of abstractness and isolation in MVC pattern and how this is achieved in ASP.NET MVC framework.

In this part I did not talk about anything related to KBlog and left all the applications of these theoretical concepts to the next part. Having a solid understanding of unit testing concepts should be an important part of your knowledge when working with MVC pattern and ASP.NET MVC framework.

In the next part of this series, I will unit test controllers in KBlog to see these theories in action.

User Comments

Title: moncler jackets outelt uk   
Date: 2012-12-03 2:49:12 AM
Title: knock off burberry scarf   
Date: 2012-12-03 2:48:34 AM
Name: Ralph Lauren Outlet
Date: 2012-11-26 6:42:49 AM
Comment: Ralph Lauren Outlet michael kors outlet
Title: Offshore Web Application Development   
Name: Domnik Desoja
Date: 2012-11-19 6:10:04 AM
Too much informative blog.You have post all details about .net and MVC.
Name: Ralph Lauren Outlet
Date: 2012-09-26 1:43:42 AM
Comment: Ralph Lauren Outlet Burberry Outlet
Title: ASP.NET Developer   
Name: Cheryl Ray
Date: 2012-09-06 2:55:28 AM
The tutorial you have explained in the blog is really amazing. I must say you have covered each and every aspect which comes in ASP.NET developer mind when comes to building Blog Engine using the mentioned technology.
Title: How can i find the part-5 of this series ?   
Name: Vu.Lam
Date: 2012-08-11 2:45:53 AM
I'v been reading this series from the 1st series. It's really healpful. I'v been trying to search for the 5-part. Where can i find it ?
Thanks a bundle
Title: Entire article the images are not shown   
Name: Skull Kid
Date: 2012-08-10 4:34:13 AM

I have been reading your article and planning on working on it. Can you kindly make sure the image links wont be broken. Its a great piece of work and it is helping me so much. I wish it does benefit many like me.
Title: burberry outlet   
Name: burberry outlet
Date: 2012-07-27 11:03:13 PM
Well, if the circumstances are very intense, the story is very moving, the character very vivid, the truth is very deep, I saw very moved. Such a good article is worth my attention, and I think will pay more attention to is should, but also can increase my knowledge.
Title: 2012 NFL jerseys   
Name: NIKE NFL jerseys
Date: 2012-05-20 11:39:17 PM
[/pre]Cheap NFL,NBA,MLB,NHL
[url=]Jerseys From China[/url]
[url=]2012 nike nfl Jerseys[/url]
[url=]cheap China Jerseys[/url]
[url=]Sports Jerseys China[/url]
[url=]NFL Jerseys China[/url]
[url=]NBA Jerseys China[/url]
NHL Jerseys China
[url=]MLB Jerseys China[/url]NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
[pre]We Are Professional China jerseys Wholesaler
[url=]Wholesale cheap jerseys[/url]Cheap mlb jerseys
[url=]2012 mlb all atar jerseys[/url]
[url= [/url]Cheap China Wholesael[/url]
[url=]Wholesale jerseys From China[/url]
[url=]2012 nike nfl Jerseys[/url]Free Shipping,Cheap Price,7 Days Deliver
We are professional jerseys manufacturer from china,wholesal
sports [url=]Jerseys From China[/url]
[url=]NFL jerseys China[/url]
[url=]NHL Jerseys China[/url]
[url=]NBA Jerseys China[/url]
[url=]MLB Jerseys China[/url]
[url=]China Jerseys[/url],Free Shipping
We are professional jerseys manufacturer from china,wholesal
sports [url=]cheap jerseys sale online [/url]
[url=]2012 nike nfl Jerseys[/url]
[url=]cheap NFL jerseys China[/url]
[url=]NHL Jerseys C
Title: Filipino chat Room   
Name: Rekoyan
Date: 2011-09-06 9:02:47 PM
Come visit the Filipino Chat Community it is the live chat community for the Philipines
Title: merawakil   
Name: alice john
Date: 2010-10-18 6:18:14 AM
The ASP.NET methodology has been a huge leap in the way web applications are developed and deployed. Previously in traditional ASP, code was cluttered and there was no clear and simplified way in which the benefits of object oriented programming could be brought to the web. In addition, there was no clear separation between the presentation layer and business logic, making maintenance of code a big issue.
Title: This is one of my favorite website. I like the information that is being share. Thanks a lot.   
Name: pacquiao vs margarito online
Date: 2010-10-06 4:48:37 PM
This is one of my favorite website. I like the information that is being share. Thanks a lot.
Title: Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or   
Name: vibram fivefingers
Date: 2010-09-13 9:56:10 PM
Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
If its common it might be difficult to get, however, be creative and you can usually find something.
Title: Colorado Business Immigration   
Name: Colorado Business Immigration
Date: 2010-09-10 12:50:01 PM
hi very nice article i got a lot of information from it.
Title: Thanks for the very nice info. It is really useful.   
Name: pacquiao vs margarito
Date: 2010-08-21 10:40:12 AM
Thanks for the very nice info. It is really useful.
Title: Offshore Web programmers   
Date: 2010-06-17 2:26:12 AM

I enjoyed it!

Title: Authentic Designer Handbags   
Name: john mite
Date: 2010-05-13 6:32:30 AM
Thanks for taking the time to share this, I feel strongly about it and love reading more on this topic. It is extremely useful for me.
Title: Yachtcharter Griechenland   
Date: 2010-04-15 2:39:29 AM
I was just thinking about Building a Simple Blog Engine with ASP.NET MVC and LINQ - Part 4 and you've really helped out. Thanks!
Title: Writing Assignment Help   
Name: Writing Assignment Help
Date: 2010-03-19 8:57:49 AM
Your blog provides us a very great information. Its really very helpful to me to find result on search engine. Hope to hear more good information related to searching from your side.
Title: nice blog i learn a lot from it. Thanks for sharing. regards, rosela   
Name: Watch Pacquiao Vs Clottey Live online
Date: 2010-03-09 6:43:14 PM
nice blog i learn a lot from it.
Thanks for sharing.


Title: blog hopping...nice blog   
Name: Webthesurfi Rugs Webdesign
Date: 2010-03-09 6:40:17 PM
blog hopping...nice blog
Title: first step   
Name: swarovski for men
Date: 2010-03-08 3:47:14 AM
Its first time here i found its very nice and informative blog. Thanks for sharing this useful post with us. i wanna to be here very often. I am going to check this site for more details
Title: your blog is very imformative and i learn a lot from it. Thanks for sharing. regards, rosela   
Name: Watch Pacquiao Vs Clottey Live
Date: 2010-02-25 9:15:22 PM
your blog is very imformative and i learn a lot from it.
Thanks for sharing.


Title: very entertaining and informative blog. Ill be back..   
Name: Webthesurfi Rugs Webdesign
Date: 2010-02-25 7:37:42 PM
very entertaining and informative blog. Ill be back..
Title: wow..i love reading your blog. i'll be back soon..   
Name: Sikat ang Pinoy
Date: 2010-02-12 5:01:11 PM
wow..i love reading your blog. i'll be back soon..
Title: sikat ang pinoy   
Date: 2010-01-29 8:40:21 PM
I like powered by because this is the free technology that i can create modern website for my personal use or for business use. I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up!
Title: hii   
Name: s
Date: 2009-10-19 12:51:50 AM
nice work..
Title: Busby SEO Test   
Date: 2008-11-29 12:30:54 AM
What a nice post about Building a Simple Blog Engine with ASP.NET MVC and LINQ
Title: Source code?   
Name: James
Date: 2008-10-31 12:21:35 PM
Great article! where is the source code?
Title: Outstanding article!ank you!   
Name: Ryan
Date: 2008-10-11 5:53:56 PM
Outstanding article Keyvan! I would love to see the completed blog. I realize there have been some changes to the framework but, the views would be a very caluable post.

Title: Nice   
Name: Nano
Date: 2008-07-25 12:56:14 PM
I can't wait any more... Next part plz!
Title: Kblog View   
Name: Shaurav
Date: 2008-07-22 12:41:55 PM
When are you going to publish the next part of it where you are going to talk about Views
Title: KBlog DEMO   
Name: Jame
Date: 2008-06-19 10:35:05 PM
Can you provide the KBlog DEMO download address?
Title: MVC   
Name: Asmi
Date: 2008-06-09 5:24:51 AM
Nice Article!! Waiting for the next to implement these practically.........
Title: MVC   
Name: Basheer Ahmed
Date: 2008-05-17 2:58:30 PM
trp to put all series in one pdf file format
Title: sweet   
Name: Johan
Date: 2008-05-13 3:59:36 PM
Nice article, looking forward for the next one.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2021  |  Page Processed at 2021-01-24 11:16:08 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search