ASP.NET OOP and Unit Testing
page 6 of 7
by Brian Mains
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 51642/ 103

Setting up an Example Page Unit Test

In the example unit test I setup, I have a base class that has a series of properties.  Some of the properties are stored in viewstate, while some return an instance of controls.  For instance, below are two properties that store their base value in the viewstate.

Listing 16

protected bool IsRedirectingFromContactPage
{
  get
  {
    object o = ViewState["IsRedirectingFromContactPage"];
    return (o == null) ? false : (bool)o;
  }
  set
  {
    ViewState["IsRedirectingFromContactPage"= value;
  }
}
 
protected bool IsRedirectingFromSubmissionForm
{
  get
  {
    object o = ViewState["IsRedirectingFromSubmissionForm"];
    return (o == null) ? false : (bool)o;
  }
  set
  {
    ViewState["IsRedirectingFromSubmissionForm"= value;
  }
}

These properties get manipulated in the initial page load, through the OnLoad method.

Listing 17

protected override void OnLoad(EventArgs e)
{
  if (this.ReferringPageName.EndsWith("contact.aspx"))
    this.IsRedirectingFromContactPage = true;
  else if (this.ReferringPageName.EndsWith("submission.aspx"))
    this.IsRedirectingFromSubmissionForm = true;
}

To ensure that one of these values get set correctly, I created a ReferringPageName property in the custom page class that returns the name of the page that is referring to it.  In the unit test, it shadows the property to return a static partial url, so that one of the properties gets set to true for testing purposes.

Listing 18

new public string ReferringPageName
{
  get
  {
    return "contact.aspx";
  }
}

In addition to setting these properties, this example custom page class sets up the data key names, creates the columns, and sets up other default settings, for the GridView control.  A Timer control also exists in the page, and the OnLoad method sets up the interval.  It would be possible to change the length of the interval to longer or shorter, based on what kind of products are being showed (if the availability may be rapidly changing, or if the cost of the product is volatile, or for some other reason).

Listing 19

this.ProductsView.DataKeyNames = new string[] { "ProductID" };
this.ProductsView.Columns.Add(this.CreateColumn("Name", "ProductName"));
this.ProductsView.Columns.Add(this.CreateColumn("Cost", "Cost"));
this.ProductsView.Columns.Add(this.CreateColumn("Availability", "IsAvailable"));
this.ProductsView.AllowSorting = true;
this.ProductsView.AutoGenerateSelectButton = true;
this.Timer.Interval = 100000;

Notice the CreateColumn method; the gridview control is setup in the custom page class, instead of the ASPX markup. This prevents duplication (in both the ASPX and the unit test), but can make some of that effort harder.  The unit test that uses this base class is below.

Listing 20

[TestFixture]
public class PageBaseTest02: PageBaseTest02_Base
{
  private Label _errorLabel = new Label();
  private Label _headerLabel = new Label();
  private GridView _productsView = new GridView();
  private Timer _timer = new Timer();
 
  protected override Label ErrorLabel
  {
    get
    {
      return _errorLabel;
    }
  }
 
  protected override Label HeaderLabel
  {
    get
    {
      return _headerLabel;
    }
  }
 
  protected override GridView ProductsView
  {
    get
    {
      return _productsView;
    }
  }
 
  protected override Timer Timer
  {
    get
    {
      return _timer;
    }
  }
 
  public PageBaseTest02()
  {
    _timer.ID = "Timer1";
    _timer.Enabled = false;
  }
 
  private DataTable BindSource()
  {
    DataTable results = new DataTable();
    results.Columns.Add("ProductID", typeof(int));
    results.Columns.Add("ProductName", typeof(string));
    results.Columns.Add("Cost", typeof(decimal));
    results.Columns.Add("IsAvailable", typeof(bool));
 
    DataRow row = results.NewRow();
    row["ProductID"= 1;
    row["ProductName"= "Super Yo-Yo";
    row["Cost"= 1.95;
    row["IsAvailable"= true;
    results.Rows.Add(row);
 
    row = results.NewRow();
    row["ProductID"= 2;
    row["ProductName"= "Silly String";
    row["Cost"= 2.25;
    row["IsAvailable"= true;
    results.Rows.Add(row);
 
    row = results.NewRow();
    row["ProductID"= 3;
    row["ProductName"= "Party Whistles";
    row["Cost"= 0.95;
    row["IsAvailable"= false;
    results.Rows.Add(row);
 
    return results;
  }
 
  [TestFixtureSetUp]
  public void Initialize()
  {
    this.OnInit(EventArgs.Empty);
    this.OnLoad(EventArgs.Empty);
 
    this.ProductsView.DataSource = this.BindSource();
    this.ProductsView.DataBind();
 
    this.OnPreRender(EventArgs.Empty);
  }
 
  [Test]
  public void TestBaseProperties()
  {
    Assert.AreEqual(truethis.IsRedirectingFromContactPage);
    Assert.AreEqual(false, this.IsRedirectingFromSubmissionForm);
    Assert.IsNotNull(this.ErrorLabel);
    Assert.IsNotNull(this.HeaderLabel);
    Assert.IsNotNull(this.ProductsView);
    Assert.IsNotNull(this.Timer);
 
    Assert.AreEqual(1, this.ProductsView.DataKeyNames.Length);
    Assert.AreEqual(3, this.ProductsView.Columns.Count);
    Assert.AreEqual(3, this.ProductsView.DataKeys.Count);
    Assert.AreEqual(3, this.ProductsView.Rows.Count);
  }
 
  [Test]
  public void TestProductsViewProperties()
  {
    Assert.AreEqual(1, this.ProductsView.DataKeys[0].Value);
    Assert.AreEqual(2, this.ProductsView.DataKeys[1].Value);
    Assert.AreEqual(3, this.ProductsView.DataKeys[2].Value);
  }
}

View Entire Article

User Comments

Title: jjjjjk   
Name: gggg
Date: 2012-10-13 3:17:31 PM
Comment:
gggg
Title: RE: Unit Test Bangs   
Name: Brian Mains
Date: 2010-04-18 10:49:40 AM
Comment:
Unit testing for simple applications can surely be done; unit tests only help strengthen an application, but unit tests add time, so for a small project, manual unit testing by the user probably is all that is necessary.

The real question is: is the application going to grow? If it will eventually become a big app, unit testing may be favorable to grow with that application.

But IMHO no you don't need unit testing for a small application, unless you want to to strengthen the application.
Title: Unit Test Bangs   
Name: Chennaite
Date: 2010-04-13 8:00:19 AM
Comment:
Nice Article. but still need to know whether we need unit testing for simple applications?..

Hats off for brain.
Title: Good Article's   
Name: LoveIndonesia
Date: 2009-10-28 5:59:38 AM
Comment:
good articles but sometime i can't understand the mean in article because my english is bad.:D
Title: wow. just what i needed.   
Name: d potter
Date: 2009-07-22 1:06:37 PM
Comment:
this article explained just what i needed to know at just the right level of detail. spot on, man.

protip: if you're stubbing both Request and Response, combine them into an IRequestResponseManager so your stub can share the same HttpCookieCollection across simulated redirects/etc.
Title: Timely info   
Name: Scheffler
Date: 2008-08-26 6:45:50 PM
Comment:
Nice article Brian. I had a similar need today and this fit the bill quite nicely. I used a pared down version of the ISessionStateManager interface you defined above because I was only barely interacting with the Session state collection. However, this has allowed me to nicely stub out and test locally my persistence logic for some prototyping work before fully fleshing out my NHibernate logic.

Thanks for taking the time to write and share your idea.
Title: Good   
Name: venkat
Date: 2008-01-25 2:43:08 AM
Comment:
its Good...! better to provide code for this.
Title: good   
Name: max
Date: 2007-10-09 3:07:04 AM
Comment:
this article is very..good...keep it up






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-09-20 4:38:26 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search