7

As a learning project, I am trying to create something similar to the WebGrid that comes with ASP.NET MVC. Now this component MyGrid<T> looks like this:

public class MyGrid<T> where T : class
{
 public MyGrid(IEnumerable<T> dataSource) { ... }
 public bool AutoGenerateColumns { get; set; }
 public MvcHtmlString HtmlMarkup { get; }
 // some more stuff
}

I have so far written some tests that test basic functionality of the grid, as well as check for some thrown exceptions. But I wonder how I should best test for the correct output, as it comes as MvcHtmlString. An example of how I checked for an empty grid is the following test:

[TestMethod]
public void AutoGenerateColumnsFalseProducesEmptyTableWhenNoColumnsAreSpecified()
{
 var grid = new MyGrid<object>(new object[] { "a", "b" });
 grid.AutoGenerateColumns = false;
 string markup = grid.HtmlMarkup.ToHtmlString();
 XDocument xdoc = XDocument.Parse(markup);
 XElement table = xdoc.Descendants().First();
 Assert.IsTrue(table.Name == "table");
 // children: thead, tbody, tfoot
 foreach (XElement child in table.Descendants())
 {
 Assert.IsFalse(child.HasElements);
 }
}

This seems quite clunky to me, plus I don't know if using XDocument is a good way to test this here. To create the markup, I use TagBuilder instead.

So to check for an empty grid is not as short/elegant as I'd like it to be. Any tips on how to make this test better are very welcome. But how would I test for more complex stuff, like to check for correct output?

The TagBuilder seems to produce some whitespace, so one way to test would be to strip the output of all whitespace and then compare with a handwritten HTML string. But again, this seems very clunky (and leaves much room for typing errors on my part).

Do you have any suggestions on how to test the (more complex) HTML output for correctness? Is there a more elegant/better way than XDocument to parse the output perhaps?

asked Aug 10, 2015 at 16:37

1 Answer 1

2

The more modern approach is to utilize the HTML Agility Pack. The syntax is closer to the dom selections that would be in a JS Library like JQuery.

Here's some examples:

This can also be loaded as a Nuget package.

Glorfindel
3,1676 gold badges28 silver badges34 bronze badges
answered Aug 14, 2015 at 12:55
1
  • Thanks for the suggestion, this looks really useful, and it's quite nice that it's so similar to jQuery. The only downside is that we don't have internet access on our development machines, which makes using NuGet impossible so far. But still this answers the question. Commented Aug 17, 2015 at 9:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.