Using the TDD Approach in MVC
Using the TDD Approach in MVC
The use of MVC, or the Model-View-Controller pattern, enables Test Driven Development. Learn to create an MVC app and test cases using TDD.
Join the DZone community and get the full member experience.Join For Free
Learn more about how CareerBuilder was able to resolve customer issues 5x faster by using Scalyr, the fastest log management tool on the market.
One of the main benefits of using MVC is its support for testability that enables the Test Driven Development approach in a simple way. An application must be created in a loosely coupled way to make it testable. Such an application allows easy testing of different application parts. The framework support is extremely important to create the testable applications. In fact, testing was a major design goal of MVC.
TDD is an approach to Test First Development in which a test is written before writing the complete production code for refactoring and fulfilling the test. It also called as the Red-Green-Refactor method of development.
The Workflow of TDD
1. Identify the requirement.
2. Write a test for automation.
3. Execute tests and ensure that the newer tests fail (red color).
4. Write the code.
5. Ensure that all tests have run and passed.
6. Perform refactoring.
7. Repeat the process.
Best Practices for Writing the Test Cases
In order to write the unit test cases, the best method is to use the F.I.R.S.T principles described as below:
Fast: The unit test cases should be written by taken their performance in consideration. It is mandatory you need to execute a large number of test cases in the release.
Isolated: Every test case should be isolated to specific behavior. That is, if a failure occurs, the programmer should be aware of what went wrong without any need to look at the flow of execution. It is recommended to break a test case in smaller parts for this reason.
Repeatable: Each test case must be stable such that we get consistent results for multiple runs.
Self-Validating: The result of a test case should either be pass or fail. It should not have any ambiguity scenarios along with assertions.
Timely: It is an important aspect for TDD since the test cases need to be created prior to the implementation.
Creating an MVC Application Using TDD
To create an MVC application by using unit test, follow the below steps:
Select "New Project" under the File menu in Visual Studio.
Go to "Installed Templates" and click on "Visual Basic" or "C#" as per the requirement and click on "Web."
Select the template of the Asp.net MVC Application.
Write the same as MvcContacts.
Click on OK.
As soon as the dialog box for Unit Test Project appears, ensure that "Yes" is selected and then select OK. The Visual Studio will create two solutions containing two projects, the first one as MvcContacts and the other named as MvcContacts.Tests.
Go the Test menu and select "Run" and then click on "All Tests of Solution." Results will be displayed in the Window of Test results. All tests are passed.
Now go to the project of MvcContacts.Tests and analyze the account controller classes for the test and the model. These classes describe the ways of creating the mock interfaces.
Creating a Database Model
Follow the below steps for creating a database model:
Go to the Solution Explorer, right-click on "App_Data folder," and select "Add." Then click on "Existing Item." A dialog box for adding an existing item will appear.
Go to the folder containing Contact.mdf file and select the file. Click on "Add."
Go to the Solution Explorer and right-click on "MvcContacts Project." Select "Add" and then "New Item." A dialog box for adding a new item will appear.
Now go to the Installed Templates and click on the Visual Basic or C# and select "Data." Click on the Entity Data Model for ADO.Net template.
Go to the Name box and enter the ContactModel. Select "Add." A wizard for the Entity Data Model will appear.
Click on the option to generate via database and select "Next."
Click on Contact.mdf as the connection to be used by the application for connecting to the database.
Ensure that the checkbox to save the settings of the connection string in web.config file is selected. Leave the default name of the connection string.
Select "Next." A wizard will appear for specifying the database objects needed to be added to the model.
Click on the node of Tables for selecting the table of contacts. Leave the namespace for default model.
Click on "Finish."
Adding a Repository
One of the best practices in MVC is to not include the code of any framework of data-access in the controller. Instead, the repository pattern needs to be used. The repository lies between the data store and the application. It separated business logic from the interactions with database and concentrates the access to data in a single area, making its creation and maintenance easier. The objects are returned from domain model by the repository. The simple models return the object from LINQ to SQL, EDM, and other models of data qualifying as the domain objects. While in the case of complex applications, there might be a requirement of a mapping layer, it is not always inefficient. We can make use of LINQ providers for creating queries for the data store at the back-end. Another point to note is that the repository does not need any knowledge of LINQ to SQL or EDM or any other type of data model to work with.
Using MVC with TDD, every test should have a particular requirement in its action method. It is not required by the test to verify the database or any other components. Another aim is that the names of the test must be descriptive because the short names will be difficult to understand with a large number of tests. For any further assistance, write to asp.net development services provider. You can even share your reviews for this post in comments.
Opinions expressed by DZone contributors are their own.