How can we do unit test in MVC (Model view controller) application (Tutorial number 6)?

Questpond
Posted by in ASP.NET MVC category on for Advance level | Points: 250 | Views : 40943 red flag

This is the 6th MVC (Model view controller) tutorial and in this article we will look in to how we can do unit testing using VSTS unit test suite.

How can we do unit test in MVC (Model view controller) application (Tutorial number 6)?


 

Introduction

This is the 6th MVC (Model view controller) tutorial and in this article we will look in to how we can do unit testing using VSTS unit test suite. In case you are completely new to MVC (Model view controller), do have a look at the previous articles and videos given in the table below for quick start.
 

Article description MVC ( Model view controller) videos MVC ( Model view controller) Article links
A simple Hello world ASP.NET MVC ( Model view controller) application. Hello word MVC Tutorial video http://www.dotnetfunda.com/articles/article1297-how-to-create-a-simple-hello-world-aspnet-mvc-tutorial-no-1-.aspx 
In this video we will see how we can share data between controller and the view using view data. MVC Tutorial video 2 Viewdata http://www.dotnetfunda.com/articles/article1310-how-to-pass-data-from-controllers-to-views-in-aspnet-mvc-tutorial-no-2-.aspx 
In this article we will create a simple customer model, flourish the same with some data and display the same in a view. MVC Tutorial video 3 Simple model and view example http://www.dotnetfunda.com/articles/article1317-how-to-create-a-simple-model-in-aspnet-mvc-and-display-the-same-tutorial-.aspx
In this article we will create a simple customer data entry screen with some validation on the view. MVC tutorial video 4 Simple customer data entry screen. http://www.dotnetfunda.com/articles/article1388-how-to-create-a-simple-aspnet-mvc-data-entry-screen-tutorial-no-4-.aspx 
This article will demonstrate how to expedite your MVC development process using HTML helper classes. NA http://www.dotnetfunda.com/articles/article1415-how-to-create-mvc-model-view-controller-views-faster-by-using-html-helper-.aspx 
This article demonstrates how we can do unit testing in MVC (Model view controller) applications.   This is the same article which you are reading currently


My half life has been spent on writing interview questions for Microsoft technology and I hope I keep writing them. You can see my video tutorial for the same by clicking on .NET,ASP.NET,C#,MVC Interview questions and answers

Click here for the Part 7 of the ASP.NET MVC article.

Click here for the Part 8 of the ASP.NET MVC article.

Click here for the Part 9 of the ASP.NET MVC article.


Overview of this article

When we started this whole MVC (Model view controller) series we started with two concerns regarding behind code:-

• How can we do unit testing on the ASP.NET behind code?

• How can we reuse the ASP.NET behind code with different user interfaces?

In this article let’s concentrate on the first point i.e. Unit testing.
Just a quick recap if we need to unit test the below method “btngenerateinvoices_click” in ASP.NET behind code , we have the following problems :-

• How do we create the sender and eventargs object?

• The below complete code runs under HttpContext object, how do I mimic them?

• What about ASP.NET UI controls, how do I access them?

• What about other ASP.NET object like session object, application, how do I access them?.

FYI: - Many developers would talk about mock test, rhino mocks etc but still its cryptic and the complication increases with session variables, view data objects, ASP.NET UI controls creating further confusion.




So what we will do in this section is  we will create a simple MVC ( Model view controller) application and we will do unit test on the ASP.NET application using VSTS unit test framework.


Step1:- Create the simple display customer screen project

The first step is to create a simple MVC ( Model view controller) project. We will use the same project which we have discussed in MVC (Model view controller) Part 5 tutorial ASP.NET MVC(Model view controller)   . So in case you do not have any sample project please create one using the link above.
 
 


The controller class at the end of the day is a simple .NET class. For instance if you watch your project code closely, you can easily see the customer controller class as shown below.

public class CustomerController : Controller
{
...
....
public ViewResult DisplayCustomer()
{
Customer objCustomer = new Customer();
objCustomer.Id = 12;
objCustomer.CustomerCode = "1001";
objCustomer.Amount = 90.34;

return View("DisplayCustomer",objCustomer);
}
}


In simple words because this is a simple .NET class we can easily instantiate the class and create automated unit tests for the same. That’s what exactly we are going to do in our next steps.

Step 2:- Add a simple unit test project

Let’s use our VSTS unit test framework to test the controller class. In case you are a complete fresher to VSTS unit testing we would request to see this article to get a hang of VSTS Unit testing  . Add a new project to your solution using the test project solution template.


 

 

 

Step 3:- Add appropriate project references


We need to add reference to the MVC (Model view controller) application in our unit test project so that we can get hold of the controller class.



 

Once you add the references you should see the MVC (Model view controller) application in your project references as shown in the below figure.
 


 

Step 4:- Write the unit test

Once you have added the references open the unit test class i.e. ‘UnitTest1.cs’. In this class create a simple test method called as ‘DisplayCustomer’ which is attributed by ‘TestMethod’ attribute as shown in the below code snippet.

If you see the below code snippet we are creating object of the controller class, invoking the controller action i.e. ‘DisplayCustomer’ and then checking if the view name is ‘DisplayCustomer’. If they are equal that means the test passes or else it fails.

[TestMethod]
public void DisplayCustomer()
{
CustomerController obj = new CustomerController();
var varresult = obj.DisplayCustomer();
Assert.AreEqual("DisplayCustomer", varresult.ViewName);
}


Step 5 :- Finally run the unit test


Once you have written your test case it’s time to run the test case by clicking on test , windows and then clicking test view.
 


On the test view right click on the test and run the selected test case as shown below.
 


If everything goes well you should see green color indicating that the test has passed or else you should see a red color with details regarding why the test failed.
 



So what’s in the next Article

In the next article we will discuss about MVC (Model view controller) routing. MVC (Model view controller) is all about connecting the actions to the controllers and MVC (Model view controller) routing helps us to achieve the same. So be ready to get routed in our next tutorial.
 

Page copy protected against web site content infringement by Copyscape

Login to vote for this post.

Comments or Responses

Posted by: Akiii on: 8/11/2011 | Points: 25
Hi sir,
I am facing a problem while unit testing the project...

varresult.ViewName

Here, it says

'System.Web.Mvc.ActionResult' does not contain a definition for 'Viewname' and no extension method 'Viewname' accepting a first argument of type 'System.Web.Mvc.ActionResult' could be found (are you missing a using directive or an assembly reference?)

Please help me with this !

Looking forward for a reply
Thanks and Regards
Akiii
Posted by: Sreeja.vijayan on: 12/13/2011 | Points: 25
Hi

Try this..

CustomerController obj = new CustomerController();
var varresult = obj.DisplayCustomer() as ViewResult;
Assert.AreEqual("DisplayCustomer", varresult.ViewName);

Hope, this will solve the issue..

Thanks!!
Sreeja
Posted by: Princess on: 2/27/2012 | Points: 25
Hello Sir,
In Custom controller I have passed objCustomer, now in UnitTest while calling DisplayCustomer() using instance of CustomerController what do I need to pass in that function. I am getting following error:
No overload for method 'DisplayCustomer' takes 0 arguments

Please help, thanks in advance

Posted by: Jhastine on: 3/9/2013 | Points: 25
Hi sir,

I run my unit test but it still running now.. I mean It takes a year. Is this normal?
Posted by: Jhastine on: 3/9/2013 | Points: 25
@Princess: you should create dummy data since in unit test we are not using UI.
Include this in your code:

CustomerController cust_controller = new CustomerController();
Customer customer = new Customer();
customer.Code = "10001";
customer.Name = "Jha";
customer.Amount = 1;

Login to post response

Comment using Facebook(Author doesn't get notification)