Illuminati
Illuminati

Reputation: 4629

What aspects can we cover Unit Testing ASP.NET MVC Views

I recently came across this nice article from David Ebbo about Unit Testing ASP.NET MVC razor views with the help of new Razor Generator tool. But I've been asking myself the question, what this can be best utilized for. Of course we can pass in a model and check if all the properties have been populated in to proper html as planned.

I'm new to this unit testing view business so need to get my head around what things to be Unit tested in razor views. Suggestions??

Upvotes: 6

Views: 625

Answers (1)

Danny Tuppeny
Danny Tuppeny

Reputation: 42443

I've been wondering the same thing. I can think of a few basic things to check, such as:

  • HTML is valid/well-formed
  • Page title (<title>/<h1>) matches the title from the model
  • Model body appears in the page
  • Correct CSS/Javascript is included
  • Important links appear correctly (paging/purchase/more info)
  • Performance (If you have lazy-loaded models, there could be performance issues you could only spot as the code in the views enumerate/access data)

In a small app, this probably doesn't add a lot of value. However in a big app, testing the views could come in handy to ensure changes things aren't becoming broken as other changes are made (despite best efforts to keep things isolated, it's not entirely unusual to break something that you believe you never made changes to!).

I think you could come up with a few important tests to ensure major things in your app work, but you could only really catch things that would be really obvious with a very quick manual test (or even caught during dev). Some may consider this worthwhile, I guess it depends on the app/team.

Previous experience has taught me that maintaining tests against an ever-changing UI sucks (and if it's not often changing, the tests won't add much value). At my last company we spent so much time trying to fix up tests that became broken with updates to the app, we couldn't add new tests (though this was in part, due to the (crap, but expensive) software we used - Mercury QuickTest). Tests written in C# would probably be more maintainable, but you still need to really weigh up the maintaince work vs the benefit you'll get from the tests.

Upvotes: 6

Related Questions