Sh Ale
Sh Ale

Reputation: 79

How to make a ASP.NET Webforms application testable?

I am looking at a legacy enterprise application, which written using ASP.NET. No controls or web forms. This is how it works:

EmployeeList.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmployeeList.aspx.cs" Inherits="EmployeeList" %>

EmployeeList.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    // Security Check

    // Load Template, get version 1.4 of active employee list
    StringBuilder template = GetTemplate("Employee", "ActiveList", "1.4", true);

    // Get list from database
    using(SqlDataReader objReader = GetListFromDB()) 
    {
        while(objReader.Read()) 
        {
            //fills data
            TUtils.Replace(template, ROW, "%name%", objReader[0]);
        }
    }

    // return response
    Response.Write(template.ToString());
}

private StringBuilder GetTemplate(string x, string y, string v, bool z);
{
    // returns template
}

private SqlDataReader GetListFromDB() {
    // returns data reader
}

My question is, since we are not using web forms, is there a way to introduce NUnit in this event driven model (as shown above)?

Also, please avoid suggestions to move to ASP.NET MVC or other patterns, which we are considering, but wondering is there any way to convert this enterprise application testable.

Upvotes: 2

Views: 939

Answers (2)

Glenn
Glenn

Reputation: 1975

This is absolutely possible. You should have a look on implementing MVP pattern with ASP.NET Webforms. There are several open source implementations but you can do a smaller specialized on your your own.

The basics are to move your code behind logic to a presenterclass. The presenter class has a reference to the page implementing an interface. The trick in your case will be to Mock the Page.Response object for your test. Thats why it´s hard to unit test it right way. The PageResponse Property contains a object deriving from HttpResponseBase and that´s the baseclass you should Mock in your tests and do your asserts on with your example. You could start with that and then extend your presenter with functionalty like Session, Request etc.

If you don´t have any markup at all probably you can just create the presenter in the view constructor and don´t bother of having and reference to the view.

To clarify: The big trick is to get the code out of the aspx.cs file. That beast is not testable.

Sample base class for Presenters:

public class Presenter<T> where T : class, IView
    {
        protected readonly T View;

        protected Presenter(T view, ILogger logger)
        {
            View = view;
        }

        public virtual void page_PreRender(object sender, EventArgs e)
        {
        }

        public virtual void page_Init(object sender, EventArgs e)
        {
        }

        public virtual void page_Load(object sender, EventArgs eventArgs)
        {
        }

        public HttpContextBase HttpContext { protected get; set; }

        protected HttpRequestBase Request
        {
            get { return HttpContext.Request; }
        }
    }

Upvotes: 4

Jagmag
Jagmag

Reputation: 10366

Since most of your code is in the code-behind, I dont think that the usual testing approach with NUnit / Visual Studio testing framework will work well.

However, I think one possible approach is to use UI Testing frameworks like WATIN / Selenium. This will enable you to still write tests for the various functionality. I have done something similar in the past by basically writing a test case for every UI action that results in a server postback. May not be ideal but it does allow you to automate your testing.

Upvotes: 0

Related Questions