Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing MVC3 Razor helpers/views without strings

I am trying out the MVC3 Razor view engine, and one the features that I am exploring is the ability to unit test views.

I have seen a number of examples where Razor views can be compiled into an assembly and rendered into a string. The problem is that it renders as a string, so I am left with doing string searches!

Here is what I am trying to do.

  1. Create a Razor helper
  2. Compile helper
  3. Run compiled helper, passing in a ViewModel
  4. Get the output of the helper as some sort of HTML/XML/tree structure

The reason that I want to do this is so that I can test for specific parts of the output. The helper will likely spit out HTML that includes various output gunk. What I want to do is to see if there is a Checkbox with a particular value (for example). If you have seen Selenium tests, it is similar to what I would like to do, except not as server driven tests.

Is there some way to get compiled Razor (or other view engine) views to emit something other than strings?

like image 551
Erick T Avatar asked Jan 29 '11 02:01

Erick T


3 Answers

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.

like image 131
Milimetric Avatar answered Nov 15 '22 16:11

Milimetric


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.

like image 29
Craig M Avatar answered Nov 15 '22 15:11

Craig M


How about rendering your views to html and then sending that html to HtmlAgility library? That way you can easily traverse/navigate your html. I don't think there is any way to do that with MVC itself.

like image 22
Egor Pavlikhin Avatar answered Nov 15 '22 17:11

Egor Pavlikhin