"ASP.net mvc with unit test example"
In this blog we will discuss how to write unit tests for the controllers in our ASP.NET
MVC applications. We will write following three Test cases:
- Test for the view returned by a controller action
- Test for the View Data returned by a controller action
- Test for whether or not one controller action redirects to a second controller action.
Testing the View returned by a Controller:
Let us first create the Controller on which we will apply the test case:
Controller Example:
using System;
using System.Web.Mvc;
namespace Store.Controllers
{
public class ProductController : Controller
{
public ActionResult Index()
{
// Add action logic here
throw new NotImplementedException();
}
public ActionResult Specification(int Id)
{
return View("Specification");
}
}
}
Note: The ProductController contains two action methods named Index() and Specification().
Both action methods return a view.
Creating the Test case:
In this we will test wether or not the ProductController returns the correct View. We will check when ProductController.Specification() method is invoked the Specification View is returned.
For this we will create a Test Class that will contain a Unit test for testing the View returned by ProductController.Specification() method.
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Store.Controllers;
namespace StoreTests.Controllers
{
[TestClass]
public class ProductControllerTest
{
[TestMethod]
public void TestView()
{
var controller = new ProductController();
var result = controller.Specification(2) as ViewResult;
Assert.AreEqual("Specification", result.ViewName);
}
}
}
Note: The above Test Class contains a method Called TestView() in which we have first created an object of ProductController, then we have invoked Specification() method and then have checked wether the Specification View is returned or not.
After writing this class we have to press Run All Tests in Solution button . If the test passes, we will see the Test result.
Testing the View Data returned by a Controller:
Let us first create the Controller on which we will apply the test case:
Controller Example:
using System;
using System.Web.Mvc;
namespace Store.Controllers
{
public class ProductController : Controller
{
public ActionResult Index()
{
// Add action logic here
throw new NotImplementedException();
}
public ActionResult Specification(int Id)
{
var product = new Product(Id, "Laptop");
return View("Specification", product);
}
}
}
Note: The ProductController contains two action methods named Index() and Specification(). In the Specification() method we have created an instance of the Product class and have passed it to the View() Method.
Creating the Test case:
In this test we will test whether or not a Product representing a laptop computer is returned when we call the ProductController.Specification() action method.
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Store.Controllers;
namespace StoreTests.Controllers
{
[TestClass]
public class ProductControllerTest
{
[TestMethod]
public void TestViewData()
{
var controller = new ProductController();
var result = controller.Specification(2) as ViewResult;
var product = (Product) result.ViewData.Model;
Assert.AreEqual("Laptop", product.Name);
}
}
}
Note: The above Test Class contains a Test Method called TestViewData(), in this we have first created an object of the ProductController class, then we have invoked the Specification() method, then we get the product by ViewData .Model property that contains the product passed to the view and at last we have checked wether or not the product contained in the View Data has the name Laptop.
Testing the Action Result returned by a Controller:
Let us first create the Controller on which we will apply the test case:
Controller Example:
using System;
using System.Web.Mvc;
namespace Store.Controllers
{
public class ProductController : Controller
{
public ActionResult Index()
{
// Add action logic here
throw new NotImplementedException();
}
public ActionResult Specification(int Id)
{
if (Id < 1)
return RedirectToAction("Index");
var product = new Product(Id, "Laptop");
return View("Specification", product);
}
}
}
Note: The ProductController contains two action methods named Index() and Specification(). In the Specification() method we have checked if the input Id is less than one then it will redirect to Index View else will create an object of Product Class and will redirect to Specification View.
Creating the Test case:
In this test we will check wether or not we will get redirected to Index View if the input Id is less than one.
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Store.Controllers;
namespace StoreTests.Controllers
{
[TestClass]
public class ProductControllerTest
{
[TestMethod]
public void TestRedirect()
{
var controller = new ProductController();
var result = (RedirectToRouteResult) controller.Specification(-1);
Assert.AreEqual("Index", result.Values["action"]);
}
}
}
Note: The above Test Class contains a Test Method called TestRedirect() in which we have first created an object of ProductController class, then we have invoked the Specification() method & passed -1 as the input parameter (Whenever we call the RedirectToAction() method in a controller action, the controller action returns a RedirectToRouteResult) and at last we checked that wether or not we are redirected to Index View.
0 Comment(s)