1. Back To Blog

MVC and .NET learning tutorials for components of MVC and how do you perform unit testing in MVC?

Components of MVC: -


MVC consist of Model, View and Controller.


  • View represents UI of the application with which end user interacts.
  • Model represents the Business Data and business Logic
  • Controller handles the User Interaction logic.


Unit testing: -


Unit testing means to test each and every small logic in our code in an independent and automated manner. Also see tutorial video on .NET explaining practically on Simple Unit Testing: -



Performing unit testing in MVC: -


When we say logic there are different kinds of logic. Business Logic, Database logic, User Interaction Logic, Presentation logic and Data Transformation logic,


  • In MVC Model or Business Logic, is simply a class with some functions. So unit testing of it is not a big deal. It can be done easily using traditional class library unit testing approach.


  • User interaction logic resides in Controller which is also a simple class. It can be tested with no much effort.
    Click here to read more about controller unit testing

  • Database logic location will be different for different people. Some people put them in the Model whereas some consider it as a separate layer in an application. In either case it's going to be simple class library and hence falls under traditional class library unit testing.

  • Remaining is Presentation Logic and Data Transformation logic. Best example of presentation logic is displaying different controls in different colours based on some dynamic value. Data Transformation logic means, converting format of data to be displayed. Example we have DateOBirth and we want to display age.
    Such kind of logic always need to be written inside the view. Unit testing of view is not an easy thing.
    Solution is Implement ViewModel concept and take out these two logic from view and put it as a separate class.
    Once it become using class library unit testing approach we can test it.
    Learn more about View Model here.


For technical trainings on various topics like WCF, MVC, Business Intelligence, Design Patterns, WPF,TFS and Basic fundamentals feel free to contact SukeshMarla@Gmail.com or visit www.sukesh-marla.com


For more stuff like this click here. Subscribe to article updates or follow at twitter @SukeshMarla

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social