Click here to Skip to main content
Click here to Skip to main content
Technical Blog

You should unit test your controller, NOT!

, 22 Jun 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
One of the MVC mantras is that it allows unit testing your controllers. But, that doesn't mean you should. In my opinion, you should focus your unit testing valuable effort in more effective areas as the controller, if done right, doesn't require unit testing.

Unit Testing MVC

Unit testing per se is a controversial topic, what you should unit test is another controversial topic. The topic of this post started by a short conversation during a .NET London Group meeting back in 2010, I thought about it and then gave it more thoughts, I started using it in my projects, then now I have enough confidence to share my thoughts. Here is my opinion, if you agree or disagree, then please do comment.

Controllers should be slim

You’ve heard this before: “Controllers shouldn’t be fat, controllers should contain minimal code”, it’s a mantra.

The S in SOLID stands for SRP. A controller should have one role, it is the communication layer between a model and a view. Even with this role, it is violating SRP, one could say maybe we need two classes to take the role of a controller or a controller per action method, but let’s not delve into that.

The controller, by role, is overloaded with responsibilities, adding more responsibilities to it is a design crime (maybe one should be penalised by losing some StackOverflow or CodeProject points Smile | :) ).

Why do controllers get fat?

I did blog earlier on how views become fat Three Steps to Get Fat-Razor MVC Views on Diet, some points are common and some are specific to controllers:

Explicit validation of data in the action method

Validation of your viewmodel data in your controller violates two principles:

  1. SRP Design Principle: You are attaching more responsibilities to the controller.
  2. Encapsulation OOP Principle: You will have to expose your data outside your viewmodel to allow another object, the controller, to validate it, while the object itself should be responsible for its own data.

ASP.NET MVC 4 has a good validation ecosystem that will help you moving your validation code to where it belongs.

Mapping a model to a viewmodel

The mapping from a model to a viewmodel should not be done in the action method. My favourite solution is to use a CQRS type query pattern so I don’t have to do this boilerplate anywhere. Here is a good Introduction to CQRS.

Leaking some business logic

This is not a place for any business logic. The controller should be calling a service, of a classical DDD implementation, or issuing a query or a command of CQRS.

Http-related code in the action method

Having a special HTTP case that requires special handling should not be done here. Action filters might be the answer.

What are you unit testing?!

Given the above and if your action methods have almost no code, then what are you unit testing? Do you want to test if you are returning the proper view or do you want to test if you have decorated your action method with the proper attributes? I wouldn’t, I usually have a deadline to hit and I am against boilerplate code.

If someone tells me their controller requires unit testing, I will ask, did you design your controller properly?

Conclusion

Unit testing is a mean for providing higher quality software and not an aim per se. Having too many redundant unit tests will cause a future maintenance headache and will cost more project development time.

If an area/layer requires unit testing then you should probably test it, but for other areas more tests may be less quality.

Update - I told you not to unit test your controllers, by keeping them empty, here is one way to keep your controllers thin: Implementing a CQRS-based Architecture with MVC and Document DB.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Adam Tibi
Architect
United Kingdom United Kingdom
Passionate about refining software practices, promoting self-motivated teams and orchestrating agile projects that hit the deadline.
Lives in London, UK and works as a .NET architect consultant in the City.
 
Need a pro service to help your organisation? Contact me via my website www.AdamTibi.net.
Follow on   Twitter

Comments and Discussions

 
QuestionWhat about simple apps? Pinmembermorzel26-Jun-13 1:33 
AnswerRe: What about simple apps? [modified] PinmemberAdam Tibi26-Jun-13 6:27 
GeneralMy vote of 5 PinprofessionalEnrique Albert24-Jun-13 1:38 
GeneralMy vote of 5 Pinprofessionalsaguptamca17-Jun-13 23:44 
GeneralMy vote of 3 PinmemberPaul Tait17-Jun-13 1:19 
GeneralRe: My vote of 3 PinmemberAdam Tibi17-Jun-13 1:31 
GeneralRe: My vote of 3 PinmemberPaul Tait17-Jun-13 3:08 
GeneralRe: My vote of 3 [modified] PinmemberAdam Tibi17-Jun-13 3:39 
GeneralRe: My vote of 3 PinmemberAdam Tibi22-Jun-13 2:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 22 Jun 2013
Article Copyright 2013 by Adam Tibi
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid