---
title: "Testing View in MVC"  
description: "In this article, I am going to demonstrates how to test the view returned by a controller action.  You can know how to test the view returned by a c"  
author: "Chris Anderson"  
published: 2011-10-06  
updated: 2020-08-08  
canonical: https://www.mindstick.com/articles/764/testing-view-in-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 3 minutes  

---

# Testing View in MVC

In this article, I am going to demonstrates how to test the view returned by a [controller action](https://www.mindstick.com/forum/12712/autocomplete-controller-action-not-being-called).\

You can know how to test the view returned by a controller action, how to test the [View Data](https://www.mindstick.com/forum/159852/view-data-from-database-using-mvc-entity-framework) returned by a controller action, and how to test whether or not [one controller](https://www.mindstick.com/forum/378/how-to-pass-value-from-one-controller-to-another-controller) action redirects you to a second controller action.

Here I am going to create [Unit Test](https://www.mindstick.com/forum/157908/what-is-a-unit-test-how-can-unit-tests-use-to-ensure-the-correctness-and-robustness-of-software) project in MVC: Open [Microsoft](https://yourviews.mindstick.com/view/81711/microsoft-disconnects-cortana-and-reinvents-it) [Visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) à New Project à ASP.NET MVC3 [Web Application](https://www.mindstick.com/articles/13069/progressive-web-application-pwas-all-you-need-to-know-about).

![Testing View in MVC](https://www.mindstick.com/mindstickarticle/008a1d09-7c0e-44dc-8c99-b7d3be07e4ab/images/0388d32b-c1af-4dac-a313-dfb41c1388dd.png)

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

![Testing View in MVC](https://www.mindstick.com/mindstickarticle/008a1d09-7c0e-44dc-8c99-b7d3be07e4ab/images/7a72a137-6a27-45b2-ade7-8997e49f049f.png)

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

![Testing View in MVC](https://www.mindstick.com/mindstickarticle/008a1d09-7c0e-44dc-8c99-b7d3be07e4ab/images/23a83a08-3a74-49c4-845c-b1309c2babb0.png)

##### ProductController.cs

The ProductController contains two action methods named Index() and Details(). Both action methods return a view. The Details() action accepts a [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) named Id. \

```
using System;
using System.Web.Mvc;
namespace Store.Controllers
{
     public class ProductController : Controller
    {
         public ActionResult Index()
         {
            throw new NotImplementedException();
         }
         public ActionResult Details(int Id)
         {
            return View("Details");
         }
    }
}
```

\

After creating a Controller, create a unit test for the controller:

Right click on the Controller folder => Add =>New Test. Select Basic Unit Test from Add New Test template.

![Testing View in MVC](https://www.mindstick.com/mindstickarticle/008a1d09-7c0e-44dc-8c99-b7d3be07e4ab/images/e147e9ae-f451-4dd7-b8fc-806e9fe1c5e3.png)

##### ProductControllerTest.cs

In **ProductControllerTest.cs** we test whether ProductController returns the right view or not. We want to make sure that when the [**ProductController.Details()**](https://www.mindstick.com/articles/75394/implement-authentication-using-identity-model-in-asp-dot-net-core) action is invoked, the Details view is returned. The test class in Listing 2 contains a unit test for testing the view returned by the ProductController.Details() action. \

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

namespace StoreTests.Controllers
{
    [TestClass]
     public class ProductControllerTest
    {
         [TestMethod]
         public void TestDetailsView()
         {
            var controller =  new ProductController();
            var result = controller.Details(2) as ViewResult;
            Assert.AreEqual("Details", result.ViewName);

         }
    }
}
```

\

If ProductController returns the matched View the result will come Passed else the result will be Aborted. Our Product controller returns the matched View that’s why Result comes Passed.

![Testing View in MVC](https://www.mindstick.com/mindstickarticle/008a1d09-7c0e-44dc-8c99-b7d3be07e4ab/images/f1fdd01a-7186-41b2-8352-aa50a88bd409.png)

The above process indicates how to test the View returned by the Controller action with the help of the Visual Studio Unit Test project.

---

Original Source: https://www.mindstick.com/articles/764/testing-view-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
