Note that there are some explanatory texts on larger screens.

plurals
  1. POTest output from HtmlTextWriter
    primarykey
    data
    text
    <p>Quite simply I want to be able to test that a Asp.Net web forms server control is outputting the correct Html as it will be building dynamic content. I'm just starting to create the control and wanted to do it in a TDD style building up the Html that gets output. </p> <p>I've created a separate class to do the actual Html building so that I am able to test it in isolation outside of the normal Asp.Net pipeline.</p> <p>In my unit tests I can control what html is returned but I'm having problems confirming that the html contains the markup I am expecting for example:</p> <pre><code>&lt;div id="wrapper"&gt; &lt;div id="fields"&gt; &lt;/div&gt; &lt;div id="main"&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>and as I'm using Nunit I would like to be able to store the output from the server control and test it by using Is.StringMatching like:</p> <pre><code>Assert.That(output, Is.StringMatching("&lt;div id=\"main\"&gt;.*?&lt;/div&gt;")); </code></pre> <p>This unfortunately won't work because of the extra \r \n \t instructions that the <code>HtmlTextwriter</code> outputs.</p> <p>The way I'm currently using it is by creating a <code>StringWriter</code> and then using it to create the <code>HtmlTextWriter</code> like this:</p> <pre><code> stringWriter = new StringWriter(); htmlTextWriter = new HtmlTextWriter(stringWriter); </code></pre> <p>and I then call the following method:</p> <pre><code>public string Build(HtmlTextWriter writer) { writer.AddAttribute(HtmlTextWriterAttribute.Id, "wrapper"); writer.RenderBeginTag(HtmlTextWriterTag.Div); writer.AddAttribute(HtmlTextWriterAttribute.Id, "fields"); writer.RenderBeginTag(HtmlTextWriterTag.Div); writer.RenderEndTag(); writer.WriteLine(); writer.AddAttribute(HtmlTextWriterAttribute.Id, "main"); writer.RenderBeginTag(HtmlTextWriterTag.Div); writer.RenderEndTag(); writer.RenderEndTag(); return writer.InnerWriter.ToString(); } </code></pre> <p>As the Html gets more complex I can forsee additional problems verifying that the output is correct.</p> <p>An additional constraint is the requirement to be able to run the tests via a CI server (most likely TeamCity) so all code or referenced libraries need to sit in my solution.</p> <p>I've done a fair bit of googling but cannot seem to find much about how to test the Html output can anybody offer any suggestions for the best way forward?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload