views:

284

answers:

2

Does anyone know how to unit test SiteMapNode? We’re building some custom navigation controls, which renders unordered html lists from Site Maps with custom attributes.

I’m trying to follow a test first approach but am finding that SiteMapNode has internal dependencies on HttpContext. To traverse the site map file it insists on using a virtual path to find the site map file.

I have build a site map, which I’d like to test against but I don’t want to create my unit testing environment in web project(!)

Any suggestions? Mocking? Overriding the relevant members?

+2  A: 

A rather dull question, so no surprise it didn't get a response! For anyone else who may stumble across this problem, here's my preferred solution:

I've found the best way to handle this is to load the physical site map into an xml document. I then have a NavigationNodeFactory, which validates and builds my own composite NavigationNode tree from the xml document.

My NavigationNode class is as opinionated as I like and circumnavigates the issues I found with SiteMap and SiteMapProvider, namely poor testability for implementers.

Ed Blackburn
A: 

I think the problem may have been that from your description, you were attempting to test-first an already existing class - the SiteMapNode.

You will want to be testing the use of the sitemap node within your application, so I would advise that if you want to perform actions on the sitemapnode class, wrap this with an intermediate ISiteMapProvider or similar abstract of your own making and have your custom navigation controls interact with this.

You can then have a SiteMapNodeProvider for runtime that calls into SiteMapNode for the operations, and a StubbedSiteMapProvider for your unit tests that pass in your test values to the custom controls.

Thank you for your response Justin. This was my initial route. Unfortunately stubbing out the SiteMaps involved writing far to much non-DRY code for liking because of composite nature of the object graph.
Ed Blackburn