Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3981902
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:27:57+00:00 2026-05-20T05:27:57+00:00

Fo unit testing ASP.NET controls I need a stubbed Page. I can create an

  • 0

Fo unit testing ASP.NET controls I need a stubbed Page.

I can create an ASP.NET Page object in my unit tests by subclassing System.Web.UI.Page.
However, I cannot find a way to set Page.Form. Adding a form with attribute (runat,server)
does not work. Overloading the form in my Subclass does not give the required functionality.

Context:
I try to unit test some homemade ASP.NET controls. These control require Page and Page.Form not to be null.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:27:57+00:00Added an answer on May 20, 2026 at 5:27 am

    Try defining an IPage and IForm interface that implement the needed methods and properties and create classes that implements those interfaces and wraps a Page or Form class. This way you can test the logic in those controls, without calling into the ASP.NET framework during unit testing.

    UPDATE:

    Overriding the page property will be brittle and is not advisable. Instead, you should try to minimize the amount of untestable code, by extracting logic in methods that don’t depend on any ASP.NET specific (hard to test) parts. Take a look at the following example:

    public class MyLabel : Label
    {
        protected override override void RenderContents(HtmlTextWriter writer)
        {
            IPage page = new PageWrapper(this.Page);
            this.MethodToTest(page);
    
            base.RenderContents(writer);
        }
    
        internal void MethodToTest(IPage page)
        {
            // Work with IPage interface.
            if (page.IsPostBack)
            {
                this.Text = string.Empty;
            }
        }
    }
    

    By extracting the logic out of methods that are hard to test, you can call those extracted methods directly in your tests. For instance:

    [TestMethod]
    public void MethodToTest_ScenarioToTest_ExpectedBehavior()
    {
        // Arrange
        var label = new MyLabel();
    
        var page = new TestPage()
        {
            IsPostBack = true
        };
    
        // Act
        label.MethodToTest(page);
    
        // Assert
        Assert.IsTrue(string.Empty, label.Text);
    }
    

    It would be even better if you would be able to extract the code under test to it’s own class and call it from your WebControl. This is however, not always possible.

    I hope this makes sense.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Unit testing and ASP.NET web applications are an ambiguous point in my group. More
I'm writing my first app with ASP.NET MVP (attempting Supervisory Controller) and Unit Testing
I'm trying to add unit testing to an ASP.NET MVC application I have built.
In my ASP.Net MVC application I am using IoC to facilitate unit testing. The
Possible Duplicate: “Web interface” to PHPUnit tests? Are there any PHP unit testing systems
I want to unit test my rendering engine for an ASP.NET app. I have
I'm looking at setting up unit tests and integration testing, but I've run into
Trying to get Nunit working in ASP.Net. The problem is, I'm testing a custom
I am working with VS 2010, Entity framework, SQl-Server 2005, ASP.Net web forms .
I am new to unit testing. Suppose I am building a web application. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.