Unit Testing ASP.NET Pages

You may need to create a unit test that calls older web code (code that is not segmented like MVC or Web API). You might also just want to create tests that call specific web pages and validate the results. Sometimes you need access to Page and Session objects in your tests, for example. In this case, you can create an ASP.NET unit test. These tests are configured to be hosted by a web server (local or IIS) and can call directly into your pages and ASP.NET environment.

To get started, add a new test project to your web solution (or create a new solution and project designed to test your website). You will also need the attribute classes found in the Microsoft.VisualStudio.TestTools.UnitTesting.Web namespace. No need to change your references in the unit test project. You simply need to add this using statement (imports in Visual Basic) to your test class file. You should now have two using statements related to testing, as shown here.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;

Next, you may want to set a reference from your unit test application to your website application. This ensures you can access the classes defined within the site. This includes the pages themselves and any other class files you might have in the App_Code directory or elsewhere.

You will also want to set a reference to System.Web. This exposes the ASP.NET components such as Page and Session.

You define three primary attributes when creating ASP.NET unit tests: UrlToTest, HostType, and AspNetDevelopmentServerHost. These attributes are defined as follows:

Image UrlToTest—This allows you to indicate a page that should be called for the execution of the given unit test. This page is called by the test framework, and the context of that web request is available to your unit test (via the Page object). You can code against the ASP.NET environment inside your unit test as if you were writing code in a web page’s code-behind file.

Image HostType—This allows you to change the host type for executing your tests to ASP.NET. You do so by passing “ASP.NET” as a string value to the attribute.

Image AspNetDevelopmentServerHost—If you are using IIS as your host, you need only set UrlToTest and HostType. If you are using the ASP.NET Development Server (that works with Visual Studio), however, you must also add the attribute AspNetDevelopmentServerHost. You pass the path to the web application as a parameter of the attribute. You also pass the name of the web application root.

Listing 8.4 shows an example of using all three attributes to define an ASP.NET unit test that runs against a local development server. Notice that you obtain a reference to the ASP.NET objects from the TestContext object’s RequestedPage property. You can use this property to cast directly to the type of the requested page (in this case, ShoppingCartPage). Of course, the RequestedPage property is of type System.Web.UI.Page and therefore gives you access to objects such as Server, Session, Request, and Response.

LISTING 8.4 An ASP.NET Unit Test


using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using System.Web.UI;
using AspNetHostedTestSample;

namespace AspNetHostedTests
{
    [TestClass]
    public class ShoppingCartTests
    {
        public TestContext TestContext { get; set; }

        [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("%PathToWebRoot%",
            "/AspNetHostedTestSample")]
        [UrlToTest("http://localhost:15279/ShoppingCart")]
        public void AddShoppingCartItemTest()
        {
            // **** README: change UrlToTest to your localhost to run ****

            //get the requested page
            Page reqPage = TestContext.RequestedPage;
            Assert.IsTrue(reqPage.Title == "Shopping Cart",
                "Page title does not match.");

            //cast to actual page type
            ShoppingCart actualPage = (ShoppingCart)reqPage;
            Assert.IsNotNull(actualPage.Cart, "There is no cart on the page.");

            //validate cart usage
            actualPage.Cart.Add("Product 1");
            actualPage.Cart.Add("Product 2");
            Assert.IsTrue(actualPage.Cart.Count == 2,
                "Item count does not match.");
        }
    }
}
}


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset