Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> MVC >> Testing View in MVC
Testing View in MVC
Testing View in MVC


by Rohit Kesharwani on 10/6/2011 5:19:39 PM

Views: 1921       Comments: 0

Testing View in MVC

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 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 in MVC

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

Testing View in MVC

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

Testing View in MVC

ProductController.cs
The ProductController contains two action methods named Index() and Details(). Both action methods return a view. The Details() action accepts a parameter 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

ProductControllerTest.cs
In ProductControllerTest.cs  we test that whether ProductController returns the right view or not. We want to make sure that when the ProductController.Details() 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 return the matched View that’s why Result comes Passed.

Testing View in MVC

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

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Rohit KesharwaniRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 3719
Advertisement
MindStick DataConver
Advertise with Us
  
Copyright © 2009 - 2014MindStick. All Rights Reserved.