In this article we will talk about creating unit testable applications using ASP.NET MVC. We will also talk a little about test driven development and see how we can design and develop ASP.NET MVC application in such a way that they are easily testable.
Test Driven Development(TDD) is gaining momentum with each passing day. The reason for this is that with TDD we can not only built robust applications but also have a proof that the application is working correctly from a functional perspective by having successfully unit test cases on our modules. Also, the projects following agile/scrum methodology find TDD very useful because the major challenge in scrum is to define when our "done" is done. If we follow TDD approach then we know that when all our test cases are passed, we are done.
ASP.NET MVC application provide a very good support for TDD because we can write unit test directly against our controllers without needing any web server. Doing so we can be sure that the controllers are working correctly from a functionality perspective.
In the rest of the article we will discuss how to design and develop MVC applications that are easily testable from unit testing perspective.
Using the code
The first thing that we need to understand is that the Controller class can be instantiated like normal classes. This is unlike the WebForms application where the pages can only be invoked by web server. This possibility of being able to instantiate the Controller classes open the door for writing test cases that will directly test the controllers logic without needing any web server.
We will now create a simple MVC application that will perform CRUD operations on a table. We will design this application in such a way that the Controller are fully testable with dummy data without even touching the actual database.
Please click here to know more!