Asp.net MVC tests custom routes

Source: Internet
Author: User

1. Download mvccontrib (http://mvccontrib.codeplex.com/releases) components

2. Add the three DLL files in the component to your test project.

 

3. In Global. asax. CS to be tested in the MVC ProjectCodeOne of them is the default route and the other is a custom route (which needs to be tested)

Public class mvcapplication: system. Web. httpapplication
{
Public static void registerroutes (routecollection routes)
{
Routes. ignoreroute ("{resource}. axd/{* pathinfo }");

Routes. maproute (
"About", // route name
"About/{ID}", // URL with Parameters
New {controller = "home", Action = "about", id = ""} // parameter defaults
);

Routes. maproute (
"Default", // route name
"{Controller}/{action}/{ID}", // URL with Parameters
New {controller = "home", Action = "Index", id = ""} // parameter defaults
);

}

Protected void application_start ()
{
Registerroutes (routetable. routes );
}
}

4. Add a class (mvcroutetest. CS) to the test project to test whether the route is correct. The Code is as follows:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using Microsoft. visualstudio. testtools. unittesting;
Using system. Web. Routing;
Using mvccontrib. testhelper. fakes;
Using system. Web. MVC;

namespace UI. tests
{< br> [testclass ()]
public class mvcroutetest
{< br> [testmethod ()]
Public void defaultroutetest ()
{< br> routecollection routes = new routecollection (); // 1. Create a routecollection.
var context = new fakehttpcontext ("~ /"); // 2. Use the fakehttpcontext class provided by mvccontrib to create an httpcontext (this code is regarded as a default route for user requests)
mvcapplication. registerroutes (routes); // 3. Call the registerroutes of the route class to be tested. routes will get the relevant route information
var routedata = routes. getroutedata (context); // 4. Locate the routing rules that meet the requirements of the created request.

// 5. Determine whether the rule meets the expectation.
assert. areequal ("home", routedata. values ["controller"]);
assert. areequal ("Index", routedata. values ["action"]);
assert. areequal ("", routedata. values ["ID"]);
}

[testmethod ()]
Public void aboutroutetest ()
{< br> routecollection routes = new routecollection (); // 1. Create a routecollection
var context = new fakehttpcontext ("~ /About/1 "); // 2. Use the fakehttpcontext class provided by mvccontrib to create an httpcontext (this code is considered as a user requesting about routing)
mvcapplication. registerroutes (routes); // 3. Call the registerroutes of the route class to be tested. routes will get the relevant route information
var routedata = routes. getroutedata (context); // 4. Locate the routing rules that meet the requirements of the created request.

// 5. Determine whether the rule is as expected
Assert. areequal ("home", routedata. Values ["controller"]);
Assert. areequal ("about", routedata. Values ["action"]);
Assert. areequal ("1", routedata. Values ["ID"]);
}
}
}

There are two test methods. The first is to test the default routing method, and the second is to test the custom about routing method.

5. How to test it?

In fact, it is very easy to put the cursor within the scope of the method to be tested. Click "Run test in the current context" or use the shortcut key Ctrl + R + T to start testing the method where the cursor is located, the test results are displayed.

Modify the code var context = new fakehttpcontext ("~ In/about/1 ("~ /About/1 ") ("~ /About "), the test result will not pass, because ("~ /About ") indicates that the request ID is the default value" ", and the assertion judgment is equal to 1, so it does not pass.

Vi. Summary

during the entire test, a Request Path is simulated and matched with the custom route to check whether the Request Path is correct.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.