vb.net code 128 font Registering the routes in a setup method in .NET framework

Creation Code128 in .NET framework Registering the routes in a setup method

Listing 26.4 Registering the routes in a setup method
USS Code 128 Creation In .NET
Using Barcode creation for ASP.NET Control to generate, create Code128 image in ASP.NET applications.
www.OnBarcode.com
GS1-128 Maker In VS .NET
Using Barcode generation for ASP.NET Control to generate, create UCC.EAN - 128 image in ASP.NET applications.
www.OnBarcode.com
[SetUp] public void Setup() { RouteTable.Routes.Clear(); MvcApplication.RegisterRoutes(RouteTable.Routes); }
Data Matrix ECC200 Printer In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create ECC200 image in ASP.NET applications.
www.OnBarcode.com
Code 128B Drawer In VS .NET
Using Barcode generation for ASP.NET Control to generate, create Code 128 Code Set C image in ASP.NET applications.
www.OnBarcode.com
With our setup method in place, our test now passes. The next scenarios we want to test are the other built-in scenarios. The tests are shown in listing 26.5.
European Article Number 13 Maker In .NET Framework
Using Barcode creation for ASP.NET Control to generate, create EAN 13 image in ASP.NET applications.
www.OnBarcode.com
Create 1D In VS .NET
Using Barcode generator for ASP.NET Control to generate, create 1D Barcode image in ASP.NET applications.
www.OnBarcode.com
Testing practices
Code 39 Full ASCII Drawer In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create Code 3 of 9 image in ASP.NET applications.
www.OnBarcode.com
Paint UPC Case Code In .NET
Using Barcode printer for ASP.NET Control to generate, create GTIN - 14 image in ASP.NET applications.
www.OnBarcode.com
Listing 26.5 Testing the built-in routing scenarios
USS Code 128 Recognizer In Visual C#.NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Code 128 Code Set B Scanner In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
[Test] public void Should_map_home_url_to_home_with_default_action() { "~/home".Route().ShouldMapTo<HomeController>(c => c.Index()); } [Test] public void Should_map_home_about_url_to_home_matching_method_name() { "~/home/about".Route().ShouldMapTo<HomeController>(c => c.About()); } [Test] public void Should_map_product_show_with_id_to_product_controller_with_parameter() { "~/product/show/5".Route().ShouldMapTo<ProductController>( c => c.Show(5)); }
EAN 128 Creation In Java
Using Barcode generation for Java Control to generate, create EAN 128 image in Java applications.
www.OnBarcode.com
EAN13 Generator In None
Using Barcode creator for Office Word Control to generate, create UPC - 13 image in Word applications.
www.OnBarcode.com
With the default scenarios added, we can now proceed with modifying our route to support the special case of a search term directly in the URL. Before we get there, though, let s make sure our routes don t already support this scenario by adding a test to verify the functionality. After all, if this test passes, our work is done! The new test is shown in listing 26.6.
Denso QR Bar Code Decoder In VB.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Draw UCC-128 In Java
Using Barcode encoder for Android Control to generate, create UCC.EAN - 128 image in Android applications.
www.OnBarcode.com
Listing 26.6 New scenario routing product search terms
Code 128 Generator In Java
Using Barcode creator for Java Control to generate, create ANSI/AIM Code 128 image in Java applications.
www.OnBarcode.com
Scan EAN 13 In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
[Test] public void Should_map_product_search_to_product_controller_with_parameter() { "~/product/SomeProductName" .Route() .ShouldMapTo<ProductController>( c => c.Search("SomeProductName")); }
ANSI/AIM Code 128 Creator In Java
Using Barcode encoder for Eclipse BIRT Control to generate, create Code 128C image in BIRT reports applications.
www.OnBarcode.com
Encoding QR Code 2d Barcode In None
Using Barcode generation for Office Word Control to generate, create Quick Response Code image in Word applications.
www.OnBarcode.com
This new test tries to prove that a route with some product name B in it will map to the Search action C, passing in the product name. Alas, our test fails, and our work isn t yet done. The test fails with the message MvcContrib.TestHelper.AssertionException : Expected Search but was SomeProductName. To make our test pass, we need to add the appropriate changes to the routes, as shown in listing 26.7.
Barcode Creation In Java
Using Barcode generation for Eclipse BIRT Control to generate, create Barcode image in Eclipse BIRT applications.
www.OnBarcode.com
Printing Quick Response Code In None
Using Barcode creation for Online Control to generate, create Quick Response Code image in Online applications.
www.OnBarcode.com
Listing 26.7 Additional route for searching products
routes.MapRoute( "SearchProduct", "product/{name}", new {controller = "Product", action = "Search"} );
Avoiding test complexity
With this addition to our routes, our new test passes, along with all the other tests. We were able to add a new route to our routing configuration with the assurance that we didn t break the other URLs. Because URLs are now generated through routes in an MVC application, testing our routes becomes of utmost importance. The test helpers in MvcContrib wrapped up all the ugliness that usually comes with testing routes. In the next section, we ll examine how to avoid unnecessary test complexity.
26.2 Avoiding test complexity
Any behavior decision an application makes must be tested, either manually or through an automated test. If we add complexity to an application, we add to the testing burden. By keeping the behavior simple, we drastically reduce the number of test cases that we have to write. This applies specifically to how routes leverage controller and action names. Although the default routes in an MVC application match a URL to a method name on a controller, the defaults can be changed. As shown in section 16.3, we can map the second URL segment to a parameter on a specific action. When using the MVC extension points of the ActionNameSelectorAttribute and ActionMethodSelectorAttribute attributes, the name of an action method on a controller doesn t exactly match the method name. The two concepts of action name and action method name are completely separate and can be configured independently. We can override the action name by applying the ActionNameAttribute as shown in listing 26.8.
Listing 26.8 Modifying the action name for an action method
using System.Web.Mvc; namespace Routes.Controllers { public class ChangedActionNameController : Controller { [ActionName("Foo")] public ActionResult Index() Action method { name return View(); } } }
Action name
In the controller shown in listing 26.8, we specified that the action method name C should be different from the action name B. The action name, originally Index, is now Foo. Navigating to /changedactionname or /changedactionname/index now results in a 404 Not Found error. The action name is now Foo, and we can only access this action through /changedactionname/foo. Because view names correspond to action names, not action method names, our view is named Foo.aspx.
Testing practices
When method names differ from action names, we can no longer use expressionbased URL generators. Without compile-time verification, URL generation is more easily susceptible to subtle refactoring and renaming errors. This can be alleviated by introducing global constants for action names, but it still creates a string-based system with another level of indirection between action methods and action names that isn t needed in many cases. In short, unless there s no other way, don t use ActionNameAttribute. In most applications, we re better served adhering to the convention that action names match action method names.
Copyright © OnBarcode.com . All rights reserved.