The earliest exposure to unit testing was to read the test-driven development mentioned in the extreme programming related materials, and then downloaded the Nunit study, but it didn't touch much, at that time, we were doing things that were time-intensive and time-consuming. The direct feeling of unit testing was that it could be time-consuming. Until I read Agile Software Development: Principles, models, and practices, the bowling scoring program inside was wonderful, and I realized that bowling was originally scored in this way, more importantly, it makes me realize that test-driven programming is meaningful and does not waste time (I will not talk about the significance of test-driven programming here, you can refer to Kent's test-driven development, Robert C. martin's Agile Software Development: Principles, models, and practices ). In webform, it is difficult to perform unit tests on the web layer logic (of course we can use the mvp mode (reference: http://www.cnblogs.com/bluewater/archive/2006/12/11/589214.html#1219888) to break down the web layer logic, but because there is no framework support, and it is not very easy to implement, nor very easy to understand, so it is not widely used ). ASP. net mvc solves the problem of testing web Logic, and testability has become one of the advantages of ASP. net mvc. Next we will introduce how to perform unit tests in ASP. net mvc.
1. Test vro:
We first use the mvc template to create a solution. We can find the following code in the Gloabal. asax. cs file:
Copy to ClipboardReference: [www.bkjia.com] public class MvcApplication: System. Web. HttpApplication
{
Public static void RegisterRoutes (RouteCollection routes)
{
Routes. IgnoreRoute ("{resource}. axd/{* pathInfo }");
Routes. MapRoute (
"Default", // Route name
"{Controller}/{action}/{id}", // URL with parameters
New {controller = "Home", action = "Index", id = ""} // Parameter defaults
);
}
Next we will write a unit test for this Code. (If you are not clear about the routing, see ASP. net mvc Practices series 1-UrlRouting)
Copy to ClipboardReference: [www.bkjia.com] 1 [TestMethod]
2 public void TestMvcApplicationRoute ()
3 {
4 RouteCollection routes = new RouteCollection ();
5 MvcApplication. RegisterRoutes (routes );
6 var httpContextMock = new Mock <HttpContextBase> ();
7 httpContextMock. Setup (c => c. Request
8. AppRelativeCurrentExecutionFilePath). Returns ("~ /Home/Index ");
9
10 RouteData routeData = routes. GetRouteData (httpContextMock. Object );
11
12 Assert. IsNotNull (routeData, "shocould have found the route ");
13 Assert. AreEqual ("Home", routeData. Values ["Controller"]);
14 Assert. AreEqual ("Index", routeData. Values ["action"]);
15 Assert. AreEqual ("", routeData. Values ["id"]);
16}
The above code is explained below
RouteCollection routes = new RouteCollection ();
MvcApplication. RegisterRoutes (routes );
The two lines generate the corresponding routes according to the format in the default routing code.
Var httpContextMock = new Mock <HttpContextBase> ();
HttpContextMock. Setup (c => c. Request. AppRelativeCurrentExecutionFilePath). Returns ("~ /Home/Index ");
The two rows mean to create a mock class of HttpContextBase. Here we use the MoQ framework to generate the mock class (refer to: MoQ (based on. net3.5, c #3.0 mock framework ).
RouteData routeData = routes. GetRouteData (httpContextMock. Object );
This line means to obtain route data from routes and prepare for the content in the asserted route below.
Assert. AreEqual ("Home", routeData. Values ["Controller"]);
Assert. AreEqual ("Index", routeData. Values ["action"]);
Assert. AreEqual ("", routeData. Values ["id"]);
These three lines verify the correctness of the Controller, action, and id generated in the route.
- Three pages in total:
- Previous Page
- 1
- 2
- 3
- Next Page