26 September 2010

ASP.NET MVC 2 Unit Testing

ASP.NET MVC frame work is designed to be very testing friendly. Here we are trying to understand the easy testing aspect of the MVC Framework.For that we are going to build our test application on top of the sample application that we tried out in part2. So recommended to have a look on that for a better understanding.

Why we need to do unit testing ?
Million dollar question! Testing is needed mainly because it's an automated way to make sure the piece of code is always behaving the way it is supposed to behave. This way we can make changes to the code and re factor pieces of the application without the fear of breaking something somewhere else in the application.This question looks silly but was the first one which came to my mind when I was writting this , hope the readeres excuse me!

Why testing is a pain with traditional ASP.NET web forms?
If you wrote unit testing in classic web application it really sucks.One of the reasons of the classic approach to developing web applications with .NET, the WebForm approach, is not easily testable: it's because the main class of ASP.NET, System.Web.UI.Page, has dependencies to HttpContext, HttpResponse, HttpRequest, HttpCookie and all the other objects coming from the fact that the Page is being executed inside a web server. And all these classes are "just" concrete classes, and cannot be replaced by fake implementations for the purpose of testing the web applications outside the context of the web server.

What makes ASP.NET MVC very good for TDD?
If the controller got a method call and it does not care about who called it then you could go and call inside of a test file and that how testing framework works. On top of this MVC made separation of components.

Code Sample?
We will discuss how to build three different types of unit tests. basically how to test the view returned by a controller action, how to test the View Data returned by a controller action, and how to test whether or not one controller action redirects you to a second controller action bla... bla... bla...
So in a nut shell  we need to test following things. 
  • Test Routes
  • Test Controller
  • Test Views.
  • Test Model
  • Test Functionality

Right click on controller folder under test project and add EmployeeControllerTest.cs.See the sample code that we need to add in this class.
[TestMethod]
public void EmployeeList_Action_Returns_View()
{
    // Arrange 
    var EmployeeController = new EmployeeController();
    // Act 
    var result = EmployeeController.EmployeeList();
    // Assert 
    Assert.IsNotNull(result, "Should have returned a ViewResult");
}
[TestMethod]
public void AddEmployee_Action_Returns_View()
{
  // Arrange 
  var EmployeeController = new EmployeeController();
  // Act 
  var result = EmployeeController.AddEmployee();
  // Assert 
    Assert.IsNotNull(result, "Should have returned a ViewResult");
}
[TestMethod]
public void TestCreateEmployee_Method()
{
   EmployeeDAL empDAL = new EmployeeDAL();
   List<EmployeeModel> employeeList = new List<EmployeeModel>();
   employeeList = empDAL.GetAllEmployees();
   // Assert 
   Assert.IsNotNull(employeeList, "Should have returned a Employee    Collection");
}
[TestMethod]
public void Add_Employee_Action_Returns_RedirectToRoute_Result()
{
   // Arrange 
   const string expectedRouteName = "EmployeeList";
   EmployeeModel emp = new EmployeeModel();
   emp.EmpName = "New Employee";
   var EmployeeController = new EmployeeController();
   // Act 
   var result = EmployeeController.Create(emp) as RedirectToRouteResult;
            // Assert 
   Assert.IsNotNull(result, "Should have returned a RedirectToRouteResult");
   Assert.AreEqual(expectedRouteName, result.RouteName, "Route name should have been {0}", expectedRouteName); 
}
Our all tests related to employee controller will be in this file.  And finally run the project and you are going to get the test results like this.

So as a conclusion we have seen some theories and code samples for building and testing MVC applications. These are the basic levels of information’s and we need to go more deep dives for better understandings. Anyway I am not quitting my  journey on MVC Framework here rather decide to take a long drive on it. In future MVC is going to rule in web development and as a developer I need to upgrade myself to this more and more. As always if you have any queries you are most welcome. 

No comments: