vb.net code 128 font If you re new to automated unit testing, be sure to pick up The Art of Unit Testing by Roy Osherove. in .NET

Maker Code 128A in .NET If you re new to automated unit testing, be sure to pick up The Art of Unit Testing by Roy Osherove.

If you re new to automated unit testing, be sure to pick up The Art of Unit Testing by Roy Osherove.
Paint Code128 In .NET
Using Barcode encoder for ASP.NET Control to generate, create Code 128C image in ASP.NET applications.
www.OnBarcode.com
Painting Data Matrix ECC200 In .NET Framework
Using Barcode generator for ASP.NET Control to generate, create DataMatrix image in ASP.NET applications.
www.OnBarcode.com
This chapter will dive into writing automated unit tests for some of the most common bits of code you ll write in an ASP.NET MVC application. We ll begin by exploring how to create automated test cases for routes.
Painting 2D Barcode In .NET
Using Barcode generation for ASP.NET Control to generate, create 2D image in ASP.NET applications.
www.OnBarcode.com
GS1 - 13 Creator In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create GTIN - 13 image in ASP.NET applications.
www.OnBarcode.com
26.1 Testing routes
GTIN - 128 Encoder In .NET
Using Barcode maker for ASP.NET Control to generate, create EAN / UCC - 14 image in ASP.NET applications.
www.OnBarcode.com
Generating Barcode In .NET
Using Barcode generation for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Routing is perhaps the biggest innovation of the ASP.NET MVC project so big, in fact, it was included in the .NET Framework 3.5 SP1 release, well ahead of the ASP.NET MVC release. With .NET 4.0, routing is merged into System.Web.dll and will be considered a core part of ASP.NET. Given that routing is part of the standard ASP.NET request pipeline just like HTTP modules, you re well served investing some time learning how to design routes for testability as well as how to test them. Like any new tool, routing is easy to abuse. Unless routes are tested thoroughly, changes to routes can break existing URLs; and changes to public URLs can break links and bookmarks, lower search rankings, and anger end users. The design of custom routes and URL patterns should come from business requirements. In this section, we ll examine some practices for testing routes to ensure we don t break our application. When we create custom routes, we need to ensure both that the routes we re creating are correct and that any existing routes aren t modified. We can start with the built-in routes and lock those down with tests. The default routes are shown in listing 26.1.
Printing Code 128 Code Set C In Visual Studio .NET
Using Barcode maker for ASP.NET Control to generate, create Code 128 Code Set B image in ASP.NET applications.
www.OnBarcode.com
Creating USPS Confirm Service Barcode In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create USPS Confirm Service Barcode image in ASP.NET applications.
www.OnBarcode.com
Listing 26.1 The default routes in a new application
Create Code 128 In Objective-C
Using Barcode generator for iPhone Control to generate, create ANSI/AIM Code 128 image in iPhone applications.
www.OnBarcode.com
Generating Code 128A In .NET
Using Barcode creator for Visual Studio .NET Control to generate, create USS Code 128 image in .NET applications.
www.OnBarcode.com
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults );
Generating Barcode In Objective-C
Using Barcode printer for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Create Barcode In Visual C#
Using Barcode generator for VS .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Testing practices
Making UCC-128 In Java
Using Barcode generation for Java Control to generate, create EAN 128 image in Java applications.
www.OnBarcode.com
Decode UCC - 12 In Visual Basic .NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
For many applications, this route is sufficient and doesn t necessarily need to be tested on its own. If we add additional routing behavior, we ll want to ensure that existing routes that follow this format aren t broken. But before we start writing tests, we need to think of a few scenarios. The URLs listed in table 26.1 should work in the default project template that ships with ASP.NET MVC 2.
Code 128A Printer In C#
Using Barcode creation for VS .NET Control to generate, create Code 128 Code Set C image in Visual Studio .NET applications.
www.OnBarcode.com
Painting 2D Barcode In Java
Using Barcode creation for Java Control to generate, create 2D Barcode image in Java applications.
www.OnBarcode.com
URL / /home /home/index /home/index/5 /home/index id=5 /home/about Result
USS Code 128 Reader In Visual Basic .NET
Using Barcode decoder for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Read UPC Symbol In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
HomeController.Index() HomeController.Index() HomeController.Index() HomeController.Index(5) HomeController.Index(5) HomeController.About()
Create DataMatrix In None
Using Barcode maker for Online Control to generate, create Data Matrix 2d barcode image in Online applications.
www.OnBarcode.com
Making Barcode In Visual Studio .NET
Using Barcode drawer for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
Table 26.1 URLS that the default sample application supports
To make things more interesting, we ll add a simple ProductController to list, view, and search products, as shown in listing 26.2.
Listing 26.2 A simplified product controller
using System.Web.Mvc; using Routes.Models; namespace Routes.Controllers { public class ProductController : Controller { public ViewResult Index() { var products = new[] { new Product {Name = "DVD Player"}, new Product {Name = "VCR"}, new Product {Name = "Laserdisc Player"} }; return View(products); } public ViewResult Show(int id) { return View(new Product {Name = "Hand towels"}); } public ViewResult Search(string name) { return View("Show", new Product {Name = name}); } } }
Testing routes
ProductController supports a List, a Show, and a Search function. Each action uses
the default view name. The actions will be exposed by the default route, but we want to support more interesting URL scenarios, like these:
/product/show/5 maps to ProductController.Show /product/SomeProductName maps to ProductController.Search(SomeProductName)
Out of the box, the built-in routes support the first scenario but not the second. Before we start messing around with our routes, we need to add tests to our existing scenarios. Testing routes is possible by creating a fake HttpContext and HttpRequest, but it s much easier with the testing extensions of the MvcContrib open source project. We ll test the first scenario from table 26.1 in listing 26.3 using these testing extensions.
Listing 26.3 Testing a blank URL
[Test] public void Should_map_blank_url_to_home() { "~/".Route().ShouldMapTo<HomeController>(c => c.Index()); }
Using extension methods contained in the MvcContrib.TestHelper namespace, the test first transforms a string into a Route object with the Route extension method. Next, we use the ShouldMapTo extension method to assert that a route maps to the Index method on HomeController. ShouldMapTo<T> is a generic method, taking an expression. It s similar to other expression-based methods, such as Html.TextBoxFor<T>. The expression is used to perform strongly typed reflection, as opposed to doing something like passing the controller and action name in as strings, which will fail under refactoring scenarios. Unfortunately, this test doesn t pass, because we haven t called anything to set up our routes. We ll accomplish this in a test setup method to be executed before every test, as shown in listing 26.4.
Copyright © OnBarcode.com . All rights reserved.