Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

like image 653
Illuminati Avatar asked Jun 27 '11 15:06

Illuminati


People also ask

What should be covered in unit testing?

A unit test typically features three different phases: Arrange, Act, and Assert (sometimes referred to as AAA). For a unit test to be successful, the resulting behavior in all three phases must be in line with expectations.

What can be used unit testing for .NET solutions?

xUnit is a free, open-source, community-focused unit testing tool for . NET. The original inventor of NUnit v2 wrote xUnit.net. xUnit.net is the latest technology for unit testing .

Why unit testing is easy in MVC?

We create the data access logic in a separate class, or set of classes, called a repository, with the responsibility of persisting the application's business model. As the Repository Pattern is useful for decoupling entity operations from presentation, it allows easy mocking and unit testing.


1 Answers

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.

like image 119
Danny Tuppeny Avatar answered Oct 07 '22 08:10

Danny Tuppeny