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(true, this.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);
}
}