views:

69

answers:

6

Is there any tangible value in unit testing your own htmlhelpers? Many of these things just spit out a bunch of html markup - there's little if no logic. So, do you just compare one big html string to another? I mean, some of these thing require you to look at the generated markup in a browser to verify it's the output you want.

Seems a little pointless.

+2  A: 

Yes.

While there may be little to no logic now, that doesn't mean that there isn't going to be more logic added down the road. When that logic is added, you want to be sure that it doesn't break the existing functionality.

That's one of the reasons that Unit Tests are written.

If you're following Test Driven Development, you write the test first and then write the code to satisfy test.

That's another reason.

You also want to make sure you identify and test any possible edge cases with your Helper (like un-escaped HTML literals, un-encoded special characters, etc).

Justin Niessner
Why are you putting logic in an html helper? Shouldn't that be in your model? Also, if you use a model for generating html (like HtmlTags or even TagBuilder) then you won't even need to worry about what output is being generated.
Ryan
@Ryan - The Model is yor data....there shouldn't be any logic there at all. Html Helpers are there strictly to help the View render the Model properly. Depending on the Model, simple logic may be needed to render things properly in the browser. Other helpers are more complicated...like Html.TextBoxFor().
Justin Niessner
@Justin "The Model is your data....there shouldn't be any logic there at all." I'm really not sure what to say to that. Either we have different ideas of what a model is, or you're working directly with DB entities. I personally prefer a rich, behavior based model. As far as html helpers, I think my most complex one is about 3 lines and it's simply calling into a templating framework I borrowed from FubuMVC.
Ryan
@Ryan - I guess I like to abstract the Logic into separate objects that act on my Model rather than including the Logic into the Model. I understand what you mean now...although rendering logic wouldn't belong in the Model anyway.
Justin Niessner
A: 

I guess it depends on how many people will be using/modifying it. I typically create a unit test for an html helper if I know a lot of people could get their hands on it, or if the logic is complex. If I'm going to be the only one using it though, I'm not going to waste my time (or my employer's money).

I can understand you not wanting to write the tests though ... it can be rather annoying to write a few lines of html generation code that requires 5X that amount to test.

Jess
A: 

it takes a simple input and exposes a simple output. This is a good one for TDD, since the time you were going to spend on build->start site->fix that silly issue->start again->oops, missed this other tiny thing->start ... we are done, happy :). Dev 2 comes along and makes small change to "fix" it for something that wasn't working for then, same cycle goes on and dev 2 didn't notice at the time it broke your other scenarios.

Instead, you v. quickly do the v. simple simple text, y that simple output gave you that simple output you were expecting with all the closing tags and quotes you were expecting.

eglasius
A: 

Having written HTML Helpers for sitemap menus, for example, or buttons for a wizard framework, I can assure you that some Helpers have plenty of logic that needs testing to be reliable, especially if intended to be used by others.

So it depends what you do with them really. And only you know the answer to that.

The general answer is that Html Helpers can be arbitrarily complex (or simple), depending on what you are doing. So the no brainer, as with anything else, is to test when you need to.

awrigley
A: 

Yes, there's value. How much value is to be determined. ;-)

You might start with basic "returns SOMEthing" tests, and not really care WHAT. Basically just quick sanity tests, in case something fundamental breaks. Then as problems crop up, add more details.

Also consider having your tests parse the HTML into DOMs, which are much easier to test against than strings, particularly if you are looking for just some specific bit.

Or... if you have automated tests against the webapp itself, ensure there are tests that look specifically for the output of your helpers.

Rodney Gitzel
A: 

Yes it should be tested. Basic rule of thumb, if it is not worth testing it is not worth writing.

However, you need to be a bit carefull here when you write your tests. There is a danger that they can be very "brittle".

If you write your tests such that you get back a specific string, and you have some helpers that call other helpers. A change in one of the core helpers could cause very many tests to fail.

So it maybe better to test that you get back a non null value, or that a specific text is contained somewhere in the return value. Rather than testing for an exact string.

Shiraz Bhaiji