views:

238

answers:

3

Is there a proper way yet to unit test views with the aspx view engine?

I've been playing with various ways that will let me get a parseable string as a result like :

view.RenderView(viewContext);

But I'm not having any luck so far.

Most of what I've read strays into integration test territory. Integration test overlap a fair bit but don't allow me to use dummy data to check the output, which is the main thing I'm trying to achieve by unit testing views.

+1  A: 

What I have done is to create objects to "drive" the user interface and placed them all into a "BusinessObjects" dll. The interface essentially pulls its results from these objects when it is time to create the interface. I then Unit test against the Business Objects using standard NUnit unit tests. I'm afraid that your question might be a bit more specific to the MVC framework but I hope that this gives you some food for thought.

Mark Brittingham
We're already doing this, it's the biggest benefit of MVC. But views can still be complicated and contain errors and we would like to test as much as possible.
flukus
+1  A: 

Have you checked out Selenium? It is directed at Web UI testing and is discussed in Hunt and Thomas' Pragmatic Unit Testing in C# with NUnit. It uses NUnit although it does so in a way that is really more of a system test than a Unit test (simply because you have to bring up the web app and navigate to your page to test it). Sorry that I didn't go down this route in my earlier answer but I was in a bit of a rush yesterday morning!

There is also NUnitAsp but that project has been essentially abandoned. It works and was widely praised in its day but I'm loathe to recommend anything that is no longer under active development.

Mark Brittingham
A: 

My advice is to not bother. Stick to testing the model and the controller. Checking for specific HTML is only going to lead to a fragile test base, that will break the moment you make the slightest change to the layout.

RichardOD