ASP. net mvc Tip #3-provide a clear view name during unit testing

Source: Internet
Author: User

Address: http://weblogs.asp.net/stephenwalther/archive/2008/06/17/asp-net-mvc-tip-3-provide-explicit-view-names-when-unit-testing.aspx

Abstract: In this Tip, Stephen Walther explains how to perform unit testing when the Controller action needs to return a specific view. It is recommended that you specify the view name if you plan to establish a unit test.

ASP. net mvc Framework is a highly testable framework. You can easily test the MVC controller Action to determine whether it can return the expected results. In this Tip, I will show you how to test a controller action that will return a specific view.

Consider the MVC controller named HomeController in Listing 1. The controller contains an action named Index. Index () action returns a view. However, the view name is not provided here. The view name is inferred from the Controller action name. Therefore, when you use Index () action, the view named Index is returned.

HomeController also contains another action named Index2. This action also returns a view. However, this action explicitly specifies the view name. The View name is passed to the View () method. The Controller action is the same as the first controller action. However, the view name of the first controller action is inferred, while the view name of the second controller action is explicitly specified.

Listing 1-HomeController. cs

1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Web;
5 using System. Web. Mvc;
6
7 namespace Tip3.Controllers
8 {
9 public class HomeController: Controller
10 {
11 public ActionResult Index ()
12 {
13 return View (); // view name inferred
14}
15
16 public ActionResult Index2 ()
17 {
18 return View ("Index"); // view name explicit
19}
20
21
22}
23}

If you plan to create a unit test for an ASP. net mvc application, you must specify the view name. Otherwise, the unit test fails even if the correct view is returned.

The test class in Listing 2 contains two test methods. The first method tests HomeController's Index () action, and the second method tests Index2 (). The first test always fails, while the second test succeeds (see figure 1 ).

Listing 2-HomeControllerTest. cs

1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Text;
5 using System. Web. Mvc;
6 using Microsoft. VisualStudio. TestTools. UnitTesting;
7 using Tip3;
8 using Tip3.Controllers;
9
10 namespace Tip3Tests. Controllers
11 {
12/*** // <summary>
13 // Summary description for HomeControllerTest
14 /// </summary>
15 [TestClass]
16 public class HomeControllerTest
17 {
18 [TestMethod]
19 public void Index ()
20 {
21 // Arrange
22 HomeController controller = new HomeController ();
23
24 // Act
25 ViewResult result = controller. Index () as ViewResult;
26
27 // Assert
28 Assert. AreEqual ("Index", result. ViewName );
29}
30
31
32 [TestMethod]
33 public void Index2 ()
34 {
35 // Arrange
36 HomeController controller = new HomeController ();
37
38 // Act
39 ViewResult result = controller. Index2 () as ViewResult;
40
41 // Assert
42 Assert. AreEqual ("Index", result. ViewName );
43}
44
45
46}
47}

Figure 1-unit test results

Unit tests cannot infer the view name. My suggestion is that if you plan to perform a unit test, you should always specify the view name explicitly.

Download the source code here: http://weblogs.asp.net/blogs/stephen enwalther/downloads/tip3/tip3.zip.

------

Advertisement: Welcome to [. NET regular expression library] http://regex-lib.net /.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.