Unit Test Cases With ASP.NET MVC

As you know ASP.NET MVC fully supports Test Driven Development approach where you can write your test cases first and on the basis of output or result you can write your actual code for development. Any Software which has their Data Access Layer or Business Layer, they should be tested before going to production. If these layers are tested it means you are going in right direction.

unit test

You can write test cases for all Public Methods, which needs to be tested.

First you just need to create an ASP.NET MVC application. To create a new ASP.NET MVC application with Test Project, Open Visual Studio choose File, New, then Project.

It will open a New Project window, from where you need to choose node as Visual C# and then Web and from the right pane you need to choose ASP.NET Web Application. Name the application.

Web application

It will open a new window from where you can choose different type of the ASP.NET MVC project template. You need to choose MVC and also be sure you have checked on Add Unit tests and click OK.

mvc

It will add both projects for you. One is your actual project and another one is your test project. In the test project, you can write your actual test cases to test the action method. See the following image which will describe the actual structure of your solution.

test case

Now open your HomeController.cs which is already created at the time of project creation with some default action method. You can see inside the HomeController.cs, there is a simple Index method, which return nothing but only a view.

controller

Now you need to go inside your Test project and you will find a default created Controller “HomeControllerTest” with test cases which is already defined in Index action method.

Test

There are some attributes and objects, used to create the test cases.

  • TestClass: This attribute defines that this class is being used for test cases and this must be defined with Test Cases Class.
  • TestMethod: It describes that the particular method which uses TestMethod attribute, is participating in Test Cases.

If you are going to test any of the method then you need to use the following steps.

  1. Arrange –You need to assign the value into the variable to test the functionality.
  2. Act – It is used to execute and capture result of the Method to take parameter value from Arrange Section.
  3. Assert – It is actual test object to compare the actual result data with your expected data.

Assert

Example

Now add a new ActionResult Method in ASP.NET MVC application and later we will make the test cases for it.

HomeController.cs

  1. public ActionResult CheckCountValue(int i)  
  2. {  
  3.    if(i<20)  
  4.    {  
  5.       //business logic goes here  
  6.    }  
  7.    else  
  8.    {  
  9.       throw (new Exception("Out of the Range"));  
  10.    }  
  11.   
  12.    return View();  
  13. }  
HomeControllerTest.cs
  1. [TestMethod]  
  2. public void CheckCountValueTest()  
  3. {  
  4.    int count = 400;  
  5.    HomeController controller = new HomeController();  
  6.    ViewResult result = controller.CheckCountValue(count) as ViewResult;  
  7.    Assert.IsNotNull(result);  
  8. }  
So, now it is time to run the test cases. Go to Test Menu and choose Run and then choose All Tests.

Note:
Please select your test project as Set startup project before running the test cases.

All test

It will go to find out all the test cases and run.

output

Error

In the above image you can see the test case is going to fail because we are passing invalid value. So, change the value from 400 to less than 20. Let’s see what happened.

Code

You will find that your test case is running successfully. So, this is the way to write your own unit test cases in ASP.NET MVC.

Thanks for reading this article, hope you enjoyed it.

 

Up Next
    Ebook Download
    View all
    Learn
    View all