In order to use Ivonna, you have to install the latest version of TypeMock Isolator.
Setting up the Web site.
- Create a Web site and add a file Default.aspx if it's not already present.
- Add a label to the page and change its ID to "HelloLabel".
- Add a textbox to the page and change its ID to "NameTextbox".
- Add a button to the page and change its ID to "HelloButton".
Setting up the test project
- Add a Class Library project to the solution.
- Add references to Ivonna, TypeMock, and your testing framework (e.g., MbUnit or NUnit) to your project.
- Change the project's output path to the Web site's bin directory.
(Note: if you want to use the Microsoft Test Framework, read this).
- Add a class to your project. Decorate it with the TestFixtureAttribute and the Ivonna.Framework.RunOnWebAttribute.
- Add a method to your class, and decorate it with the TestAttribute.
- Each test method should begin with obtaining a reference to a TestSession instance.
- Execute a GET request usingt this instance and obtain a reference to the page using the GetPage method.
- Find the textbox and set its text to "John".
- Execute a postback request using the session's ExecutePostback method and obtain a reference to the page.
- Find the HelloLabel label and assert that it's text is equal to "Hello John".
The full code should look like this:
VB.Net:
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports MbUnit.Framework
Imports Ivonna.Framework
<TestFixture(), RunOnWeb()> _
Public Class WebTester
<Test()> Public Sub HelloTest()
Dim session As New TestSession()
Dim testPage As Page = session.GetPage("Default.aspx") 'Get the page
Dim NameTextbox As TextBox = testPage.FindControl("NameTextbox") 'Find the textbox
NameTextbox.Text = "John" 'Enter some data
testPage = session.ProcessPostback("HelloButton") ' That's how we process postbacks
Dim label As Label = testPage.FindControl("HelloLabel") 'Find the label
Assert.AreEqual("Hello John", label.Text)
End Sub
End Class
C#:
using System.Web.UI;
using System.Web.UI.WebControls;
using MbUnit.Framework;
using Ivonna.Framework;
[TestFixture, RunOnWeb]
public class WebTester
{
[Test] public void HelloTest() {
TestSession session = new TestSession();
Page testPage = session.GetPage("Default.aspx"); //We first GET our page
TextBox NameTextbox = (TextBox) testPage.FindControl("NameTextbox"); //Find the textbox
NameTextbox.Text = "John"; //user enters some data
testPage = session.ProcessPostback("HelloButton"); // That's how we process postbacks
Label label = (Label) testPage.FindControl("HelloLabel");
Assert.AreEqual("Hello John", label.Text);
}
}