In previous lesson, we have learnt how ASP.NET MVC URL routing works and in this chapter, we will learn how we can do test driven development with ASP.NET MVC.
What is Test Driven Development?
Test-driven development is an approach to write test code first before you move your application to production environment. You need to write some code to test your application and if test fails, you need to re-factor some code of your application and then once again you need test your application. Until all written code test is passed, this iteration will go on. This kind of TDD also known as Reg Green Refactor Development.
Followings are interaction of Test Driven Development:
- Identify your requirement and develop code for that.
- Write code for automated testing
- Run your test with your test framework and see if any test fails.
- If one of your tests fail, then refcator your code and write code new code.
- Again, perform test and see if passed successfully.
- Repeat all the steps above until your whole step passes.
Unit Tests:
Unit tests are number of tests performed by developers before moving code to production environment.
We need to decide where we need to write Unit tests, as we don’t have to write unit tests that are very slow. For example, we don’t have to write unit tests for database layer as it is relatively slow operation and also we don’t have to write unit tests for view operation as this is again a slow environment to write unit test code.
Getting started with TDD and ASP.NET MVC
So, what we are waiting for let us start our development with TDD Approach. So let us create a new ASP.NET MVC Project from File-> New Project environment once you select the ASP.NET MVC 3 application it popup as following dialog.

Here, you can see there is a check box to create unit test project so we have enabled it, as we need to test driven development. We also need to specify the Test Driven Development Project and which testing framework we are going to choose. Right now for the time being, we are going to use Visual Studio Unit test only.
Once you click Ok, it will create two projects in the solution as the following:

So now, project creation is completed. Here for testing purpose, we are going to use same data table user for the testing, which we have used in earlier version. Following is the structure for the table:

We have inserted following data in the table:

Now I am going to create entity model classes with the help of entity framework and then will create user controller and views from with the help of MVC Scaffolding which we have discussed in earlier chapters. After creating User Controller, now it’s time to write unit tests.
To do I have selected a project and via right click -> Add new Test following dialog box will appear.

Once you click OK, It will create a unit test classes as following.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MvcApplication4.Tests.Controllers
{
/// <summary>
/// Summary description for UserControllerUnit
/// </summary>
[TestClass]
public class UserControllerUnit
{
[TestMethod]
public void TestMethod1()
{
//
// TODO: Add test logic here
//
}
}
}
Here you can see the code it has created a test method where we are going to write some test logic there. So, I have slightly changed this coding as follows:
using System.Collections;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvcApplication4.Controllers;
namespace MvcApplication4.Tests.Controllers
{
/// <summary>
/// Summary description for UserControllerUnit
/// </summary>
[TestClass]
public class UserControllerUnit
{
[TestMethod]
public void TestMethod1()
{
//Arrange
var userController = new UserController();
//Act
var result = (ViewResult) userController.Index();
//Assert
Assert.IsInstanceOfType(result.ViewData.Model,typeof(IEnumerable));
}
}
}
Here in the above code, you can see in Test Method1, I have created a object of user controller and then I have created User Controller Index view result variable to see a set of users are returned or not and Assert.IsInstaceOfType verify that whether its returning a list of users are or not.
Now it is time to run our tests. To do that, you need go to the test menu ->Run-> Tests in current context. Once you run your test, it will fail as below:

So let us see why this test is failed. I have seen that there was problem with my connection string and so I have modified my connection string and run that test once again and now it is passed.
So, it is very use to crate test driven application. Hope you like it.