views:

97

answers:

2

I know that Microsoft and MvcContrib have made great attempts at enabled developers to unit test controllers. But lately I've been wondering if we should ditch unit testing controllers, and leave them for integration testing. I have two reasons for this:

  • A good controller should not have any logic in it, or very basic minimal logic. Certainly not business logic. It should be small and compact, and dependent on application services to do what it needs to do to. If those services are well tested, what do I have left to test in my controller?
  • The controller is basically the way that users interact with your application. This, just like a UI, is highly subject to change. For example, I may decide one day that a particular action belongs in a different controller. To be sure, if we adopt the paradigm of a "controllerless action", this point is probably not as powerful.
  • Am I off base here? Looking at other peoples code, I see people very much into unit testing controllers.

    +1  A: 

    I don't always unit test the simplest MVC controllers either, but I've found a couple cases where the tests are beneficial (at least in Java Spring MVC):

    1. When I create a controller that modifies something in the user's session (for example, adding a session attribute that's used throughout the site). It just verifies that the proper data gets added to the session.
    2. When the controller must perform an authentication/authorization check to decide which view to show. I add 2 test cases, 1 where the user session has proper credentials (assert that they're forwarded to the desired view), and 1 where it does not (assert that they're forwarded to the login view).
    Kaleb Brasee
    +1  A: 

    Check out this post for more info.

    In short, testing your controllers is great to check to see whether the (process) is working but they provide little value in terms of debugging i think.

    I prefer to unit test my (units) so that i know i am getting expected results from each.

    Then I'll test units in sequence and finally the controller but only for completeness.

    Getting propper tests written from your units nets you far better debugging information than a high level message from say your controller.

    griegs
    Does it make sense to test a controller twice, once as a SUT (just to make sure that you're passing in the correct data, let's say), and the other as an integration test, or functional test?
    blockhead
    Yeah I think it does. The unit tests will test individual functionality and also provide you with a means to test if the unit was broken when other code changed. The integration test then tests the end-to-end process. Ultimatly what the user experiance will be. Not all controllers will need to be tested in this way but complex ones which may include many PartialViews may need their own tests. It's up to you as to the granularity of the tests you want to write and how comfortable you are with your controller code.
    griegs