With the following code behind.
Create a unit test with the following code to test it.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
namespace AspUnitTest.Test
{
///
/// Summary description for DefaultAspxTest
///
[TestClass]
public class DefaultAspxTest
{
private TestContext testContextInstance;
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}
[TestMethod()]
[HostType("ASP.NET")]
[UrlToTest("http://localhost:50551/default.aspx")]
[AspNetDevelopmentServerHost("C:\\Users\\brian.orrell.PARIVEDA\\Documents\\Source\\Samples\\AspUnitTest\\AspUnitTest")]
public void TestNameEntry()
{
Page page = testContextInstance.RequestedPage; //1
TextBox NameTextBox = (TextBox)page.FindControl("NameTextBox");
Assert.IsNotNull(NameTextBox);
Button SubmitButton = (Button)page.FindControl("SubmitButton"); //2
Assert.IsNotNull(SubmitButton);
Label MessageLabel = (Label)page.FindControl("MessageLabel");
Assert.IsNotNull(MessageLabel);
PrivateObject po = new PrivateObject(page); //3
string name = "Brian Orrell";
NameTextBox.Text = name;
po.Invoke("SubmitButton_Click", SubmitButton, EventArgs.Empty);
Assert.AreEqual(string.Format("Hello, {0}", name), MessageLabel.Text);
}
}
}
Pretty easy.