Skip to Content
Testing ASP.NET Web Applications
book

Testing ASP.NET Web Applications

by Ben Hall, Jeff McWherter
October 2009
Intermediate to advanced
430 pages
12h 45m
English
Wrox
Content preview from Testing ASP.NET Web Applications

3.2. Unit Testing ASP.NET WebForms

An ASP.NET application is made up of two main parts. The aspx file contains the ASP.NET markup, which is a mixture of HTML, ASP.NET controls, and ASPX tags that contain code to be executed on the server before being returned to the user. The second part is the aspx.cs file that is the code-behind for a particular page. The code-behind files contain C# code to support the main ASP.NET page, allowing you to hook into the page lifecycle and execute code on the server when the page loads, or more commonly when a user requests some data that needs to be processed by the server.

However, because these code-behind files hook into the lifecycle of the page they have a tight dependency to the core ASP.net runtime. When it comes to testing, this tight dependency causes a huge amount of problems. If you attempt to access the object outside of the ASP.net runtime, such as when writing unit tests then you will receive a number of error messages in and around the core engine. When it comes to the aspx file, any of the code placed in the view is not accessible from another class — as such you wouldn't be able to write any unit tests for it.

In order to be able to unit test ASP.NET WebForms you need to think about how to develop the application in a different way. The most important thing to remember is to keep the view and the code-behind classes as thin as possible. They should contain the minimum amount of code to function and as soon as the code starts to ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Pro ASP.NET Web API: HTTP Web Services in ASP.NET

Pro ASP.NET Web API: HTTP Web Services in ASP.NET

Tugberk Ugurlu, Alexander Zeitler, Ali Kheyrollahi
ASP.NET 4 Unleashed

ASP.NET 4 Unleashed

Stephen Walther, Kevin Hoffman, Nate Dudek

Publisher Resources

ISBN: 9780470496640Purchase book