Unit testing MVC3 Razor helpers/views without strings?

The short answer is no, because view engines' purpose in life is to spit out strings. Parsing those strings into an XML document is a way to give them a little structure, like @Craig-M suggested. But what you have to ask yourself is what you're really testing.

If your view compiles and generates some kind of HTML, there can be three problems with what it generated.

The short answer is no, because view engines' purpose in life is to spit out strings. Parsing those strings into an XML document is a way to give them a little structure, like @Craig-M suggested. But what you have to ask yourself is what you're really testing.

If your view compiles and generates some kind of HTML, there can be three problems with what it generated: the data it displays is not correct. You would test for this at the controller level, so you can ignore it during view testing. The MVC framework had an error and it generated the wrong HTML string.

You don't worry about this, because MVC has its own test suite and thanks to separation of concerns, it's not your problem. The HTML has broken the user interface. That last one would be very nice to test for, but why not test for it in javascript unit tests?

Check out popular javascript unit test suites like JsUnit, FireUnit, QUnit, etc. They would all do a much better job than you could do of parsing out Razor output. I think the value assigned to a checkbox would probably be tested for in Controller testing. But to use your example, testing your checkbox's value could be $('#theCheck').val() == 'the value'.

And you can run these against a website running with dependency injected repositories or services to control things like 'the value'. Just a thought.

Thanks for the excellent answer. The reason for testing at the HTML level is to test logic in the view. For example, if I pass in a ViewModel that contains Products, and has a property of in stock.

If a product has stock == 0, don't show a purchase link. Later, I add logic in the ViewModel to ensure that products never have stock == 0, but instead use IsInStock? Property.

I want a test that will fail due to it not producing the right HTML. One way to write this test is to construct a legit ViewModel, pass it to the view, and check that the link is/is not generated. Make sense?

Thanks, – Erick T Feb 19 '11 at 19:31 Yeah, I follow. Checking that the link exists in QUnit would be something simple like: test("testing for link", function() { ok( $('#myLink'). Length == 1, "link present" ); }); – Milimetric Feb 20 '11 at 18:58.

One way you could go about this is to parse the html string into an XDocument and have your assembly return it. Then you can query against it with LINQ in your tests. Edit: I'm trying to figure out a Razor test strategy too.

I'd be interested to know how you get the helper to work. So far, I'm drawing a blank in getting them rendered to strings outside of the MVC framework.

That is the direction I am going right now. Even with Razor, it is kind of a hassle to spin up get all the dependencies working right (e.g. , TempData) for testing a view. I might look into other View Engines, but I think that it be ASP.

NET MVC that is the problem. Let me know if you make any progress! Thanks – Erick T Feb 19 '11 at 19:33 I spent two days trying various different methods to get the views rendered to a string.

No luck so far. Every path came up against some sort of resistance such as was needing to instantiate an internal class or not being able to use the MVC helpers. Since we're already doing WebAii testing on this project, I ran out of time to make it work and had to abandon my effort.

I'm very interested in doing this on my own projects though and will definitely post back here if/when I come up with a solution. – Craig M Feb 21 '11 at 20:36 A tad late, but I posted parts to get a working unit test harness for Razor views in the following question: stackoverflow.com/questions/8720222/… – Lucero Jan 4 at 0:27.

That way you can easily traverse/navigate your html. I don't think there is any way to do that with MVC itself.

This is the direction that I ended up going. With the rise of HTML5, it looks like expecting real XML is going to a problem later. – Erick T Mar 9 '11 at 8:06.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions