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
Unit testing is, roughly speaking, testing bits of your code in isolation with test
Unit testing sounds great to me, but I'm not sure I should spend any
Unit testing with C/C++: What do you teach people who either did not do
I've heard that unit testing is totally awesome, really cool and all manner of
Does Python have a unit testing framework compatible with the standard xUnit style of
So I know that unit testing is a must. I get the idea that
How are people unit testing their business applications? I've seen a lot of examples
How are people unit testing code that uses Linq to SQL?

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.