articles

Home / DeveloperSection / Articles / Testing View Data in MVC

Testing View Data in MVC

Chris Anderson15285 06-Oct-2011

In this article, I am going to demonstrates how to test the View Data returned by a controller action

You can know how to test the view returned by a controller action, how to test the View Data returned by a controller action, and how to test whether or not one controller action redirects you to a second controller action.

Here I am going to create Unit Test project in MVC:
Open Microsoft Visual Studio Ă New Project Ă ASP.NET MVC3 Web Application.

Testing View Data in MVC

Select Internet Application from New ASP.NET MVC3 Project template and select Visual Studio Unit Test for the Test Framework.

Testing View Data in MVC

Delete pre-existence file from a Controller folder that exists in .Tests extension.

Testing View Data in MVC

After deleting the pre-existence file, Create a Model class Product.cs, in which property named Products is created that return the product passed to the constructor of Product class.

using System.Collections.Generic; 
namespace UnitTestMVC.Models
{
     public class Product
    {
         string prd = "";
         public string Products
         {
            get { return prd; }
            set { prd = value; }
         }
         public Product(int Id, string prd)
         {
            this.prd = prd;
         }
    }
}

In the below ProductController.cs Index and Details actions are implemented.
The Details() action creates a new instance of the Product class that represents a Cotton fabric. Next, the instance of the Product class is passed as the second parameter to the View() method.
using System; 
using System.Web.Mvc;
using UnitTestMVC.Models;

namespace Store.Controllers
{
     public class ProductController : Controller
    {
         public ActionResult Index()
         {
            throw new NotImplementedException();
         }

         public ActionResult Details(int Id)
         {
            var product = new Product(Id, "Cotton");
            return View("Details", product);
         }
    }
}

  In the below ProductControllerTest.cs TestDetailsView() method tests the View Data returned by invoking the Details() method. The ViewData is exposed as a property on the ViewResult returned by invoking the Details() method. The ViewData.Model property contains the product passed to the view. The test simply verifies that the product contained in the View Data has the name Cotton. 

using System.Web.Mvc; 
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Store.Controllers;
using UnitTestMVC.Models;

namespace StoreTests.Controllers
{
    [TestClass]
     public class ProductControllerTest
    {

         [TestMethod]
         public void TestDetailsViewData()
         {
            var controller = new ProductController();
            var result = controller.Details(2) as ViewResult;
            var product = (Product)result.ViewData.Model;
            Assert.AreEqual("Cotton", product.Products);
         }
    }
}


If ProductController returns the matched View data the result will come Passed else the result will be Aborted. Our Product controller returns the matched View Data.

Testing View Data in MVC

The above process indicates how to test the View Data returned by the Controller action with the help of Visual Studio Unit Test project.


Updated 04-Mar-2020
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By