Skip to content

Your First Test

Jeffrey T. Fritz edited this page Feb 10, 2016 · 4 revisions

Your first test will inspect that a property on a page has been set properly during the Load event. With a web form code-behind that looks like the following:

public class _Default : Page {

  public _Default() {

    Load += Page_Load;

  }

  protected void Page_Load(object sender, EventArgs args) {

    LoadEventTriggered = true;

  }

  public bool LoadEventTriggered = false;

}

You can then test that the LoadEventTriggered property is properly set to true with a unit test written in MSTest that looks like:

[TestClass]
public class UnitTest1
{
  [TestMethod]
  public void TestMethod1()
  {

    // Arrange

    // Act
    var sut = new FirstTest.Default();
    sut.FireEvent(WebFormsTest.TestablePage.WebFormEvent.Load, EventArgs.Empty);

    // Assert
    Assert.IsTrue(sut.LoadEventTriggered, "Load event was not triggered");

  }
}

The sut variable name refers to "System Under Test" and will be used in this documentation to refer to those classes that are being tested. The Default page now has access to the TestablePage.FireEvent method that can be used to trigger the standard Page events. The Assert method call then verifies the logic in the Page_Load event handler to ensure that the LoadEventTriggered property is properly set to true

Try this sample for yourself in the First Test solution in the Samples folder.

In your code, this could be checking much more complex business logic to ensure that it is calculating properly for your application.

Clone this wiki locally