I just want to verify that the Asp.Net web form server server is displaying the correct Html as it will generate dynamic content. I am just starting to create a control and would like to do it in TDD style by creating the Html that will be output.
I created a separate class to make the actual Html building so that I can test it in isolation outside the normal Asp.Net pipeline.
In my unit tests, I can control the html return, but I am having trouble verifying that the html contains the markup that I expect, for example:
<div id="wrapper"> <div id="fields"> </div> <div id="main"> </div> </div>
and since I use Nunit, I would like to be able to store the output from the server control and validate it using Is.StringMatching, for example:
Assert.That(output, Is.StringMatching("<div id=\"main\">.*?</div>"));
This, unfortunately, will not work due to the additional \ r \ n \ t commands that come out of the HtmlTextwriter .
The way I'm using it now is to create a StringWriter and then use it to create an HtmlTextwriter as follows:
stringWriter = new StringWriter(); htmlTextWriter = new HtmlTextWriter(stringWriter);
and then I call the following method:
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(); }
As HTML becomes more complex, I can anticipate additional issues that confirm the output is correct.
An additional limitation is the requirement to be able to run tests through a CI server (most likely TeamCity), so all code or link libraries should be in my solution.
I have worked a bit with the search engine, but cannot find much information on how to test the Html output. Can anyone suggest any suggestions for the best way forward?