Rohith
Rohith

Reputation: 2091

Unit testing modularized ASP.Net webforms pages

I'm currently working on a legacy application which uses ASP.Net web forms. Pages in this application have a mixture of formatting and logic in the aspx and ascx files. Typically things like:

if (number of passengers > 1)
  include singlePassengerForm.ascx
else 
  include multiPassengerForm.ascx

but think a lot more complicated - nested ascx includes, lots more logic, much bigger pages, etc.

I would like to fix this, but slowly. The final design I'm currently driving towards is a very light page/control which won't have any logic, but would just pick things from a model (including which ascx to embed). I like the fact that the pages and controls are broken down into smaller pieces and I'd like to keep that. So this would translate to an interconnected model (eg: ReservationFormModel has a PassengerFormModel - which could be a SinglePassengerForm or a MultiplePassengerForm) and a set of interconnected pages with probably a set of controllers to populate the model and connect the model and the page. So sort of MVCish. Basic idea being, I want to unit test that logic.

The other thing I want to do is fix this as an on going process - not in one big bang. So I'd like to be able to start small, where I fix just one part of a page while the rest of the page still uses the old way of embedded logic and data in the aspx/ascx.

Any ideas/suggestions/experience reports around this would be really appreciated.

I've seen the answers to this: What is the best way to do unit testing for ASP.NET 2.0 web pages? and this: Unit Testing Legacy ASP.NET Webforms Applications but I'm looking for something more specific

Upvotes: 1

Views: 236

Answers (1)

danyolgiax
danyolgiax

Reputation: 13106

As you know WebForm path is not the most testable way to write code. A key aspect of MVC pattern is in fact that controllers can be tested much better.

A simple solution is to include core logic in "Logic" classes like MVC Controllers and use page behind code only to bind data to controls. You can then use, for example, NUnit framework to create test fixture and NCover to monitor test code covering.

Upvotes: 1

Related Questions