Robert Koritnik
Robert Koritnik

Reputation: 105029

When should Asp.net MVC views be unit tested

Suppose you have a project with many developers working on the same Asp.net MVC application. Project is a long run one, so team members will change over time. This means that during this time we get to a point where certain views seem (totally) different to others. Markup wise.

In order to keep the UI as consistent as possible over time let's say that initial team develops a sort of fluent Html helper methods, for creating generic view structure. Ie:

Html.CreateHeader("Header text")
    .CreateForm(Url.Action(...))
    .AddSegment("Person")
    .EditorFor(model => model.Person)
    .AddSegment("...")
    .EditorFor(model => model.Company)
    ...

Although this does add an additional abstraction over everyday Asp.net MVC view code, but it makes views consistent over time. Of course when these fluent helpers are used.

But. Even though each of these helper methods can be unit tested I wonder what good it is to test views themselves?

Questions

  1. My opinion is that you don't unit test anything static (ie. views). So unless views have rich client side functionality, nothing should be tested. But if it does, you would be testing Javascript code with ie. QUnit library. Is this thinking correct or does it really make sense to test something that is intrinsically static in nature and we'd only be testing presence of certain elements and their content?

  2. What other aspects of views can be tested and why should they be tested?

During my development I've noticed that unit testing should only be done on non-trivial code. In other words methods that have at least one branch should be tested, and others that are long enough to not be completely obvious what they do even without branches.

I tend to avoid writing unit tests for methods that are trivial in nature, because they don't aid anything except consuming developer's time.

Upvotes: 0

Views: 136

Answers (1)

Dan
Dan

Reputation: 3258

Views should only be presenting the data that is provided to them, therefore there shouldn't be any logic to test anyway.

In my opinion the "testing" of views comes under integration testing. (Checking everything looks ok, links go to the right places etc.)

Upvotes: 2

Related Questions