Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save miklund/6ff7a9bf8b62c02a6fc9 to your computer and use it in GitHub Desktop.
Save miklund/6ff7a9bf8b62c02a6fc9 to your computer and use it in GitHub Desktop.
2011-01-26 How to UnitTest your WebControls rendered HTML
# Title: How to UnitTest your WebControls rendered HTML
# Author: Mikael Lundin
# Link: http://blog.mikaellundin.name/2011/01/26/unittest-how-your-webcontrols-render.html
var ul = string.Format("<ul{0}{1}>",
string.IsNullOrEmpty(cssClass) ? string.Empty : string.Format(@" class=""{0}""", cssClass),
string.IsNullOrEmpty(xmlns) ? string.Empty : string.Format(@" xmlns=""{0}""", xmlns));</ul{0}{1}>
const string Xmlns = "urn:unordered-radio-button-list";
var control = new Template { DataSource = data, Xmlns = Xmlns };
<?xml version="1.0" encoding="utf-8" ?>
<ul xmlns="urn:unordered-radio-button-list">
<li>
<input id="bananas" name="bananas" type="radio" value="Bananas" />
<label for="bananas">I like bananas</label>
</li>
<li>
<input id="onions" name="onions" type="radio" value="Onions" />
<label for="onions">I like onions</label>
</li>
</ul>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="UnorderedRadioButtonList"
targetNamespace="urn:unordered-radio-button-list"
elementFormDefault="qualified"
xmlns="urn:unordered-radio-button-list"
xmlns:mstns="http://www.litemedia.se/UnorderedRadioButtonList.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Root element -->
<xs:element name="ul">
<xs:complexType>
<!-- Unbounded number of elements -->
<xs:sequence maxOccurs="unbounded">
<xs:element ref="li" />
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- The LI element-->
<xs:element id="li" name="li">
<xs:complexType>
<xs:sequence>
<xs:element ref="input" />
<xs:element ref="label" />
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- input element has only attributes -->
<xs:element id="input" name="input">
<xs:complexType>
<xs:attribute name="id" type="xs:string"></xs:attribute>
<xs:attribute name="name" type="xs:string"></xs:attribute>
<xs:attribute name="type" type="xs:string" fixed="radio"></xs:attribute>
<xs:attribute name="value" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<!-- label contains for attribute and content -->
<xs:element id="label" name="label">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="for" type="xs:string"></xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
namespace LiteMedia.Web.UI.WebControls.Test
{
using System.IO;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using NUnit.Framework;
using System.Xml.Schema;
[TestFixture]
public class UnorderedRadioButtonListShould
{
/* This private class is used to reach protected members in our SUT */
private class Template : ULRadioButtonList
{
public new void Render(HtmlTextWriter htmlTextWriter)
{
base.Render(htmlTextWriter);
}
}
[Test]
public void ProduceExpectedHtmlMarkup()
{
/* Prepare some static variables */
const string Xmlns = "urn:unordered-radio-button-list";
var schema = GetType().Namespace + ".UnorderedRadioButtonList.xsd";
/* Radio button list data source
* <input type="radio" value="Bananas" /><label>I like bananas</label>
* <input type="radio" value="Onions" /><label>I like onions</label>
*/
var data = new ListItemCollection
{
new ListItem("I like bananas", "Bananas"),
new ListItem("I like onions", "Onions")
};
/* SUT */
var control = new Template { DataSource = data };
control.DataBind();
/* Prepare render output stream */
var htmlOutputStream = new MemoryStream();
var htmlOutputStreamWriter = new StreamWriter(htmlOutputStream);
var htmlWriter = new HtmlTextWriter(htmlOutputStreamWriter);
/* Test */
control.Render(htmlWriter);
htmlWriter.Flush();
/* Assert */
htmlOutputStream.Position = 0; /* Reset stream */
/* Load the schema */
var xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(schema);
var xmlSchema = XmlReader.Create(xsdStream);
/* Create xml reader and set schema for validation */
var settings = new XmlReaderSettings();
settings.Schemas.Add(Xmlns, xmlSchema);
settings.ValidationType = ValidationType.Schema;
// Trigger this event on schema validation errors
settings.ValidationEventHandler += HandleValidationErrors;
// Read and validate htmlOutputStream
var xmlValidatingReader = XmlReader.Create(htmlOutputStream, settings);
while (xmlValidatingReader.Read()) ;
}
private void HandleValidationErrors(object sender, ValidationEventArgs e)
{
// There was validation errors, fail with message
Assert.Fail(e.Message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment