This article will focus on how to build a specific type of unit test in a test-driven development environment when building an ASP. net mvc Web application project.
In fact, not all unit tests are excellent TDD tests. To apply unit tests in test-driven development, you must be able to execute unit tests very quickly. However, not all unit tests can meet this requirement.
For example, Visual Studio provides a specific type of unit test support for ASP. NET websites. You must perform unit tests of this type in the context of IIS or the development web server. However, when you conduct test-driven development, this is not a proper type of unit test, because the unit test speed of this type is too slow.
In this article, I want to show you the detailed process of building a unit test for test-driven development. I will describe in detail about how to use the Visual Studio 2008 unit test framework. In addition, I will discuss several advanced topics, such as testing private methods and how to execute tests from the command line.
Quickly create an ASP. net mvc Web application project example
First, let's create a new ASP. net mvc Web application project and create a corresponding test project. This step is very easy. When you create a new ASP. net mvc Web application project, the system will prompt you whether to create a New Visual Studio test project, as shown in 1. As long as you keep the single button at the top of Figure 1, that is, the default option), you will see a new test project automatically added to your solution.
Now the question is: Since you have a test project, how do you use this test project?
When you create a new ASP. net mvc application, the project includes a controller named HomeController. This controller has two default methods named Index () and About () respectively. The HomeController project provides a test project named HomeControlleterTest. This test file contains two test methods: Index () and About ().
By default, the content of the Index () and About () test methods is empty (2 ). Next, you can add your test logic in these methods.
Suppose we want to build an online storage system. For example, you want to create a Details page to display the Details of a specific product. Then, you need to pass a query string containing ProductId to this Details page, retrieve product Details from the database, and display the information to the page.
In good test-driven development practices, you need to write a test before coding. Instead of writing any application code, you must first compile the test corresponding to the code. To create a successful Details page, the following test requirements must be met:
1) if a ProductId is not passed to this page, an exception should be thrown.
2) The ProductId should be used to retrieve a product from the database
3) if a matched product cannot be retrieved from the database, an exception should be thrown.
4) The Details view should be generated smoothly
5) The Product data should be assigned to the ViewData structure of the Details View.
The above introduces ASP. net mvc Web Application Project
- Analysis on PageBase and MasterPage of ASP. NET
- XML Web Service Method of ASP. NET
- Detailed description of ASP. NET environment deployment
- Implement ASP. NET globalization
- Solve the Problem of ASP. net ajax script errors