MVC - Does a module test the wrong things?

While working on some TDDs to work on an ASP.Net MVC project, I came across several scenarios where I wrote tests to ensure that certain actions returned the correct views or had certain attributes ( [ChildActionOnly] , etc.)., (in fact, I found here some interesting SO posts about useful extension methods to help achieve this).

When I first became acquainted with the concepts of unit testing and TDD, when I was in the course several years ago, the emphasis was on the fact that the tests should focus on the testing logic, the desired functions and functionality - the main requirements of the project if you do this.

My question is if this is so, do the cell checks validate the correct view file to be visualized, or an action that has a specific attribute, etc., that does not really cover the unit testing methodology? Am I writing tests for the wrong reasons (i.e. just protecting myself and other colleagues from refactoring errors) or are these valid cases of valuable unit tests?

+7
source share
4 answers

If a handler method can return one of two (or more) representations depending on some logic, then unit test is useful, which would assert the correct representation. The same applies to the handler method, which inserts certain attributes depending on the logic.

Am I writing tests for the wrong reasons (i.e. just protecting other colleagues due to refactoring errors) or are these valid cases of valuable unit tests?

Clever regression errors are one of the advantages of unit tests, especially useful when refactoring. If you inadvertently changed the view returned when doing some refactoring, which would be very useful for early detection, and not for waiting for a test that was performed only when the application was launched.

+5
source

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

If you always return View (), then do not do this.

If you return a view with a name different from your action name, then you can argue that it would be nice to write a test.

+2
source

It depends. To use one of your examples: checking whether an action has a specific attribute is good, but it's better to write a test that checks that the behavior is working properly, that it won’t work if the attribute is missing.

However, ultimately your tests are a defense network. If he has reasonable chances to prevent the error from slipping at some point in the future, he does his job.

If this is a simple test with low overhead and a small chance of arbitrary hacking in the future, go for it. I would rather have too many tests than too few.

+1
source

Indeed, your tests should test your logic. This should not go away. However, ideally, you should write tests for anything that could go wrong. For example, make sure that all methods you need to provide have the appropriate Authorize attribute. This is a security test.

Ultimate, you decide that it’s good for you to test.

+1
source

All Articles