Unit Testing ASP.NET User Controls
page 2 of 5
by Brian Mains
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 30019/ 90

Test Setup

There are two ways to setup a unit test for a user control. The first is to have the unit test inherit from the target, as illustrated below. Note that the user control base class (AssignmentUserControlBase) is defined as an abstract class.

Listing 1

[TestFixture]
Public void AssignmentUserControlBaseTest : AssignmentUserControlBase { }

Each user control test implements the abstract properties/methods of the base class. Essentially, this custom base class sits between the System.Web.UI.UserControl class and the ASCX code-behind page. The benefit to this is that the AssignmentUserControlBase class exists in a class library, compiled as a DLL, which can be used in unit testing.

The alternative approach is to create a unit test that instantiates an instance of the user control.  I am not going to use this approach because it makes it harder to unit test the user control.  Some of the benefits will be discussed later.


View Entire Article

User Comments

Title: mr   
Name: rob
Date: 2012-03-15 4:55:09 AM
Comment:
would love a complete solution with usercontrol project and test projects for reference.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-29 10:23:23 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search