1

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

Community
  • 1
  • 1
Rohith
  • 2,043
  • 1
  • 17
  • 42

1 Answers1

1

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.

danyolgiax
  • 12,798
  • 10
  • 65
  • 116
  • I feel this doesn't really answer my question. To clarify: I already have a code base on my hand which uses logic in the code behind. I want to be able to move towards a tested controller/model approach, but I don't have the time to do it in a big bang fashion. Within a single page/control, I'd like to be able move just one part of the logic (which I need to change) to a controller, write tests for that and then make my change. Do you have any suggestions for this? – Rohith May 31 '11 at 11:31