Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC - Unit testing the wrong things?

Whilst practicing some TDD at work for an ASP.Net MVC project, I ran into a number of scenarios where I was writing tests to ensure that particular actions returned the correct views or had particular attributes on them ([ChildActionOnly] etc). (in fact, I found a number of interesting posts here SO about useful extension methods to help acheive this).

When I was first introduced to the concepts of unit testing and TDD when on a course a few years ago, the emphasis was based strongly on that tests should be focusing on testing logic behind the user-desired features and functionality - the core project 'requirements' if you will.

My question is - if this is the case, are menial tests checking for the correct view file to be rendered, or an action having a particular attribute etc not really encompassing what the unit testing methodology is all about? Am I writing tests for the wrong reasons (i.e. simply protecting myself and other colleagues from making a refactoring mistake) or are these valid cases of valuable unit tests?

like image 401
harman_kardon Avatar asked Jan 27 '12 14:01

harman_kardon


4 Answers

If a handler method could return one of two (or more) views depending on some logic then a unit test that asserted the correct view would be useful. Same goes for a handler method that inserted particular attributes depending on the logic.

Am I writing tests for the wrong reasons (i.e. simply protecting other colleagues from making a refactoring mistake) or are these valid cases of valuable unit tests?

Catching regression errors is one of the benefits of unit tests, especially usefull when refactoring. If a you inadvertently changed the view returned while doing some refactoring that would be very usefull to catch early - rather than waiting for a test that only ran when the application was runnning.

like image 163
blank Avatar answered Sep 21 '22 23:09

blank


If your action returns different views (or action results) depending on some logic. Write a test.

If you always return View() then don't.

If you return a view with a name different from your action's name - then you could argue it would be a good idea to write a test.

like image 30
MartinHN Avatar answered Sep 19 '22 23:09

MartinHN


It depends. To use one of your examples: Checking if an action has a particular attribute is okay, but it's better to write a test that verifies a behavior is functioning as expected, that would fail if the attribute was missing.

That said, ultimately your tests are a safety net. If it has a reasonable chance of preventing a bug from slipping in at some point in the future, it's doing its job.

If it's a simple test that has low overhead and a low chance of breaking arbitrarily in the future, go for it. I'd rather have too many tests than too few.

like image 29
Daniel Mann Avatar answered Sep 18 '22 23:09

Daniel Mann


Indeed, your tests should be testing your logic. That should not go away. However, ideally you should write tests for anything that can go wrong. For instance, insuring that all methods that need to be secured have a proper Authorize attribute. This is a security test.

Ultimate, you decide what's useful for you to test.

like image 40
Erik Funkenbusch Avatar answered Sep 19 '22 23:09

Erik Funkenbusch