At CodeGarden 10 I did a presentation on Unit Testing with Umbraco which was primarily looking at doing Unit Testing with ASP.NET and then have you can take those principles into doing development with Umbraco.
Unfortunately the session ran way over time, but we have a good open space the following morning to look deeper into the stuff I didn’t have a chance to cover.
##Unit Testing with Umbraco
When doing unit testing with Umbraco there’s a few things you need to take into account:
- Reliance on the HttpContext
- Static methods
Because of these things it’s quite hard to stub out a type which is reliant on static methods it’s quite a tricky thing, you really need to use some kind of an isolation framework like Typemock. And if you’re relying on the HttpContext then you need to either spin up Cassini/ IIS, or try and mock it out.
NodeFactory is a tricky beast, it expects the XML cache, so if you don’t have it where it thinks it should be, then it’s not going to make your life easy.
###Looking into Snapshot
In the past I’ve blogged via my work blog, FarmCode.org, we’re working on a new product called Snapshot which is designed to push out a plain ASP.NET website with no Umbraco reliances at all. During CodeGarden 10 we decided to release part of Snapshot for free, the CMS API, which is designed to abstract away the Umbraco aspect and gives you the ability to do unit testing.
Snapshot exposes most of what can be done with NodeFactory, Media and
umbraco.library, but does so via interfaces. This means that they can be stubbed out and used for testing.
##Working with ASP.NET WebForms MVP
So I wasn’t just using the standard ASP.NET WebForms MVP install, I was also using the Contrib project which I contribute on. I was using this for the Autofac integration, as I wanted to be able to dependency inject more than just the view.
##Resources from the presentation
Here’s what you’ll need from my presentation to be able to dig into it yourself:
Hopefully this gives you a good start for doing unit testing your own Umbraco development.