Code samples

Common test structure

The only difference is that you have to put the RunOnWeb attribute on each test class. Typically, you want to create an instance of the TestSession class. See the examples below (NUnit):

//C#
 
using Ivonna.Framework;
using Ivonna.Framework.Webforms; //Ivonna.Framework.MVC for MVC sites
using
 NUnit.Framework; namespace CSWebTestProject1 { [TestFixtureRunOnWeb] public class TestFixture1 { [Test] public void Test() { var session = new TestSession();
// // TODO: Add test logic here // } } }
'VB.Net
 
Imports NUnit.Framework
Imports Ivonna.Framework
Imports
 Ivonna.Framework.Webforms 'Ivonna.Framework.MVC for MVC sites


 <TestFixture()> _ Public Class TestFixture1 <Test(), RunOnWeb()> _ Public Sub Test() Dim session As New TestSession() TODO: Add test logic here End Sub End Class

Several ways to issue a GET request

//C#
var session = new TestSession();

// WebForms only System.Web.UI.Page page = session.GetPage("Default.aspx");
// All frameworks
WebResponse response = session.Get("Users/List");
// Low-level, most flexible
WebRequest request = new WebRequest("Default.aspx"); response = session.ProcessRequest(request);
' VB.Net
 
Dim session = New TestSession()
 
' WebForms only
Dim page As Web.UI.Page = session.GetPage("Default.aspx")

' All frameworks Dim response As WebResponse = session.Get("Users/List") ' Low-level, most flexible Dim request = New WebRequest("Default.aspx") response = session.ProcessRequest(request)

Issuing a simple POST request

//C#
 
var session = new TestSession();
// WebForms only
var page = session.GetPage("Default.aspx");
var page2 = session.ProcessPostback("Button1");
 
// Other frameworks
var response = session.Post("Users/Add"new {field1 = "value1", field2 = 27}); 
' VB.Net
 
Dim session = New TestSession()
 
' WebForms only
Dim page = session.GetPage("Default.aspx")
Dim page2 = session.ProcessPostback("Button1")
 
'Other frameworks
Dim response = session.Post("Users/Add",
	New With {.field1 = "value1", .field2 = 27})

Asp.Net MVC functionality

//C#
 
using System.Web.Mvc;
using Ivonna.Framework;
using Ivonna.Framework.Mvc;
using MbUnit.Framework;
using SampleWeb.Models;
 
namespace SampleTestsCS {
[TestFixtureRunOnWeb]
public class SimpleRequests {
/// <summary>
/// Given the request URl and posted data,
/// we can get controller and action names,
/// input parameters,
/// action result, view name, model value etc.
/// </summary>
[Test]
public void PostRequest() {
	var response = new TestSession().Post("/Home/Show/1",
		new InputModel { SomeString = "smth" });
	Assert.AreEqual("Home", response.ControllerName);
	Assert.IsInstanceOfType<ViewResult>(response.ActionResult);
	Assert.AreEqual("Show", response.ViewName);
 
	var viewData = ((ViewResult) response.ActionResult).ViewData;
	Assert.AreEqual("Welcome to ASP.NET MVC!", viewData["Message"]);
 
	Assert.AreEqual(1, response.ActionMethodParameters["id"]);
	Assert.IsInstanceOfType<InputModel>(
		response.ActionMethodParameters["input"]);
	Assert.AreEqual("smth", 
		((InputModel) response.ActionMethodParameters["input"]).SomeString);
	Assert.IsInstanceOfType<OutputModel>(response.Model);
	Assert.AreEqual("result", ((OutputModel) response.Model).SomeString);
}
}
}
'VB.Net
 
Imports MbUnit.Framework
Imports Ivonna.Framework.Mvc
Imports System.Web.Mvc
Imports SampleWeb.Models
 
''' <summary>
''' Given the request URl and posted data,
''' we can get controller and action names,
''' input parameters,
''' action result, view name, model value etc.
''' </summary>
''' <remarks></remarks>
<TestFixture(), RunOnWeb()> _
Public Class SimpleRequests
   <Test()> _
   Public Sub PostRequest()
      Dim response = New TestSession().Post("/Home/Show/1", 
                                            New InputModel() With { _
       .SomeString = "smth" _
      })
      Assert.AreEqual("Home", response.ControllerName)
      Assert.IsInstanceOfType(Of ViewResult)(response.ActionResult)
      Assert.AreEqual("Show", response.ViewName)
 
      Dim viewData = CType(response.ActionResult, ViewResult).ViewData
      Assert.AreEqual("Welcome to ASP.NET MVC!", viewData("Message"))
      Assert.AreEqual(1, response.ActionMethodParameters("id"))
      Assert.IsInstanceOfType(Of InputModel) _
         (response.ActionMethodParameters("input"))
      Assert.AreEqual("smth", 
                      DirectCast(response.ActionMethodParameters("input"), 
                          InputModel).SomeString)
      Assert.IsInstanceOfType(Of OutputModel)(response.Model)
      Assert.AreEqual("result", 
                      DirectCast(response.Model, OutputModel).SomeString)
   End Sub
End Class

Latest news

Latest blog posts

Powered by FeedBurner