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.
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.