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 WebTest project to the solution, found in the Test project template folder.
- Switch off Shadow Copying (Deployment in MsTest) in your test runner
(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 Ivonna.Framework
Imports MbUnit.Framework
Imports Ivonna.Framework.WebForms
<TestFixture(), RunOnWeb()> Public Class WebTester
''' <summary>
''' We verify that we can get a valid reference to the Page object.
''' </summary>
<Test()> Sub CanGetThePage()
Dim session As New TestSession 'Start each test with this
Dim page As Web.UI.Page = session.GetPage("Default.aspx")
Assert.IsNotNull(page)
End Sub
''' <summary>
''' We verify that if we enter "John" in the textbox
''' and click the button, the text of the label becomes "Hello John".
''' </summary>
<Test()> Sub HelloLabel_Displays_HelloJohn()
Dim session As New TestSession
'Get the page before writing to the textbox.
Dim testPage As Web.UI.Page = session.GetPage("Default.aspx")
Dim textBox As Web.UI.WebControls.TextBox =
testPage.FindControl("NameTextBox")
textBox.Text = "John" 'Write something
'When we click a button, a new Page object is created.
testPage = session.ProcessPostback("HelloButton")
Dim label As Web.UI.WebControls.Label =
testPage.FindControl("HelloLabel")
Assert.AreEqual("Hello John", label.Text)
End Sub
End Class
C#:
using System.Web.UI;
using System.Web.UI.WebControls;
using Ivonna.Framework;
using Ivonna.Framework.WebForms;
using MbUnit.Framework;
namespace SampleTests
{
[TestFixture, RunOnWeb()]
public class WebTester {
/// <summary>
/// We verify that we can get a valid reference to the Page object.
/// </summary>
[Test]
public void CanGetThePage() {
TestSession session = new TestSession(); //Start each test with this
System.Web.UI.Page page = session.GetPage("Default.aspx");
Assert.IsNotNull(page);
}
[Test]
public void HelloLabel_Displays_HelloJohn() {
TestSession session = new TestSession();
//Get the page before writing to the textbox.
Page page = session.GetPage("Default.aspx");
TextBox textBox =
page.FindRecursive<TextBox>("NameTextBox");
textBox.Text = "John"; //Write something
//When we click a button, a new Page object is created.
page = session.ProcessPostback("HelloButton");
Label label =
page.FindRecursive<Label>("HelloLabel");
Assert.AreEqual("Hello John", label.Text);
}
}
}
Note: you can see the MVC samples at the
Code Samples page.