.net code 128 barcode Testing action filters in Visual Studio .NET

Generating Code 128 in Visual Studio .NET Testing action filters

26.5 Testing action filters
Painting Code 128B In VS .NET
Using Barcode maker for ASP.NET Control to generate, create Code 128 Code Set C image in ASP.NET applications.
www.OnBarcode.com
Matrix Creator In VS .NET
Using Barcode printer for ASP.NET Control to generate, create 2D image in ASP.NET applications.
www.OnBarcode.com
Testing action filters is similar to testing model binders. Unit testing is possible, and its difficulty is directly proportional to how much the filter relies on the context objects. Generally, the deeper the filter digs into the context object, the more that will need to be set up or mocked in a unit test. Table 26.2 illustrates the types of filters and the context objects used for each. Each context object has its own difficulties for testing and its own dependencies for usage. All context objects have a no-argument constructor, and a unit test may be able to use the context object as is without needing to supply it with additional objects. Although our filter may use only one piece of the context object, we may find ourselves needing to supply mock instances of more pieces, because many of the base context object constructors have null argument checking. We may find ourselves far down a long path that leads to supplying the correct dependencies for a context object, and these dependencies may be several levels deep.
Painting PDF 417 In .NET
Using Barcode creation for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
Barcode Drawer In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Testing action filters
Data Matrix ECC200 Drawer In .NET Framework
Using Barcode generator for ASP.NET Control to generate, create Data Matrix ECC200 image in ASP.NET applications.
www.OnBarcode.com
Encoding UPC-A In .NET
Using Barcode creator for ASP.NET Control to generate, create UPC Symbol image in ASP.NET applications.
www.OnBarcode.com
Table 26.2 Filters and their supporting context objects Filter type Method Context object
Generating Barcode In .NET
Using Barcode maker for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Generate 2/5 Industrial In .NET
Using Barcode creator for ASP.NET Control to generate, create 2/5 Standard image in ASP.NET applications.
www.OnBarcode.com
OnActionExecuted IActionFilter OnActionExecuting IAuthorizationFilter IExceptionFilter IResultFilter OnResultExecuting OnAuthorization OnException OnResultExecuted
Painting Code 128 In Objective-C
Using Barcode printer for iPad Control to generate, create Code128 image in iPad applications.
www.OnBarcode.com
Reading Code 128B In Visual Basic .NET
Using Barcode reader for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
ActionExecutedContext ActionExecutingContext AuthorizationContext ExceptionContext ResultExecutedContext ResultExecutingContext
GTIN - 13 Recognizer In Visual C#.NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Print Code 128 Code Set B In Objective-C
Using Barcode generation for iPad Control to generate, create Code-128 image in iPad applications.
www.OnBarcode.com
Let s add tests to the filter shown in listing 26.18.
Encode Code 3/9 In Visual Studio .NET
Using Barcode generation for VS .NET Control to generate, create Code 3 of 9 image in .NET applications.
www.OnBarcode.com
Encoding DataMatrix In Java
Using Barcode creation for Java Control to generate, create DataMatrix image in Java applications.
www.OnBarcode.com
Listing 26.18 Creating a simple action filter
Making EAN128 In Java
Using Barcode creator for Java Control to generate, create EAN / UCC - 14 image in Java applications.
www.OnBarcode.com
Recognize EAN13 In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
public class CurrentUserFilter : IActionFilter { private readonly IUserSession _session; public CurrentUserFilter (IUserSession session) { _session = session; } public void OnActionExecuting(ActionExecutingContext filterContext) { ControllerBase controller = filterContext.Controller; User user = _session.GetCurrentUser(); if (user != null) { controller.ViewData.Add(user); } } public void OnActionExecuted(ActionExecutedContext filterContext) { } }
Quick Response Code Drawer In Java
Using Barcode creator for Java Control to generate, create QR Code JIS X 0510 image in Java applications.
www.OnBarcode.com
UPC-A Supplement 2 Recognizer In VS .NET
Using Barcode scanner for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
In this filter, we have the requirement that a User object is needed for a component in the view, likely for displaying the current user in a widget. Our CurrentUserFilter depends on an IUserSession, whose implementation contains the logic for storing and retrieving the current logged-in user from the session. Our filter retrieves the current user and places it into the controller s ViewData. The controller is supplied through the ActionExecutingContext object. If possible, during unit testing, we prefer to use the no-argument constructor and supply any additional pieces by merely setting the properties on the context object. The ActionExecutingContext type has setters for the Controller property, so we ll be able to use the no-argument constructor and not worry about the larger, parameterfull constructor.
Encode EAN128 In None
Using Barcode printer for Software Control to generate, create EAN / UCC - 14 image in Software applications.
www.OnBarcode.com
Data Matrix ECC200 Encoder In Java
Using Barcode generator for Java Control to generate, create Data Matrix image in Java applications.
www.OnBarcode.com
Testing practices
Our complete unit test, shown in listing 26.19, is able to create a stub implementation for only the parts used in our filter.
Listing 26.19 Action filter unit test
using using using using using using using System.Web.Mvc; MvcContrib; NUnit.Framework; Rhino.Mocks; UnitTestingExamples.Helpers.Filters; UnitTestingExamples.Models; UnitTestingExamples.Services;
namespace UnitTestingExamples.Tests { [TestFixture] public class CurrentUserFilterTester { [Test] public void Should_pass_current_user_when_user_is_logged_in() { var loggedInUser = new User(); var userSession = MockRepository .GenerateStub<IUserSession>();
Creates stub IUserSession
userSession.Stub( session => session.GetCurrentUser()) .Return(loggedInUser);
Stubs result of GetCurrentUser
var filterContext = new ActionExecutingContext { Controller = MockRepository Sets .GenerateStub<ControllerBase>() controller };
var currentUserFilter = new CurrentUserFilter(userSession); currentUserFilter .OnActionExecuting(filterContext); var user = filterContext .Controller.ViewData.Get<User>(); Assert.AreEqual(loggedInUser, user); } } }
Invokes filter
Asserts correct results Creates ActionExecutingContext
Our CurrentUserFilter depends on an implementation of an IUserSession interface B, which we supply using Rhino Mocks. Next, we stub the GetCurrentUser method on our IUserSession stub to return the User object created earlier C. Because the implementation of IUserSession requires the full HttpContext to be up and running, supplying a fake implementation gives us much finer control over the inputs to our filter object.
Summary
Next, we create our ActionExecutingContext D but call only the no-argument constructor. The controller can be any controller instance, and we again use Rhino Mocks to create a stub implementation of ControllerBase E. Rhino Mocks creates a subclass of ControllerBase at runtime, which saves us from using an existing or dummy controller class. In any case, the ControllerBase provides ViewData, so we don t need to provide any stub implementation for that property. With our assembled ActionExecutingContext and stubbed implementation of IUserSession, we can create and exercise our CurrentUserFilter F. The OnExecutingMethod doesn t return a value, so we need to examine only the ActionExecutingContext passed in. We assert that the controller s ViewData contains the same loggedin user created earlier G, and our test passes! Getting to this point required trial and error to understand what the context object required for execution. Because filters are integrated and specific to the MVC Framework, it can be fruitless to try to write filters using test-first test-driven development only the fact that the complete website is up and running proves the filter is working properly. We supplied dummy implementations of the context objects, but we constructed them in a way that the MVC Framework will likely not use.
26.6 Summary
In this chapter, we looked at testing some of the most popular types of code we ll write with the ASP.NET MVC Framework. We learned how to test routes using the test helpers available in MvcContrib. We also learned how to create automated tests for controllers, model binders, and action filters. Each of these types of code has special behaviors, and each of these needs automated test cases. Because code can be executed on every request, it s vital to ensure that code behaves as desired. The true test of a working MVC application is using it in a browser. Refer back to chapter 20 on full-system testing for more than just unit testing. In chapter 27, we ll learn how to apply jQuery to create an autocomplete text box.
Copyright © OnBarcode.com . All rights reserved.