vb.net code 128 font Testing the Edit action when errors are present in VS .NET

Generating Code 128 in VS .NET Testing the Edit action when errors are present

Listing 26.12 Testing the Edit action when errors are present
Code 128A Generator In Visual Studio .NET
Using Barcode printer for ASP.NET Control to generate, create ANSI/AIM Code 128 image in ASP.NET applications.
www.OnBarcode.com
QR Code ISO/IEC18004 Creator In VS .NET
Using Barcode generation for ASP.NET Control to generate, create QR Code JIS X 0510 image in ASP.NET applications.
www.OnBarcode.com
[Test] public void Edit_should_redirect_back_when_model_errors_present() { var badProduct = new Product {Name = "Bad value"}; var repository = MockRepository.GenerateStub<IProductRepository>(); var controller = new ProductsController(repository); controller.ModelState .AddModelError("Name", "Name already exists"); ActionResult result = controller.Edit(badProduct); Assert.AreEqual("", result.AssertViewRendered().ViewName); repository.AssertWasNotCalled( rep => rep.Save(badProduct)) }
Creating DataMatrix In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create ECC200 image in ASP.NET applications.
www.OnBarcode.com
Drawing Barcode In VS .NET
Using Barcode printer for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Sets up controller for test
Barcode Generator In .NET Framework
Using Barcode drawer for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Encode 2D Barcode In VS .NET
Using Barcode printer for ASP.NET Control to generate, create Matrix image in ASP.NET applications.
www.OnBarcode.com
Asserts correct results
EAN 13 Creation In VS .NET
Using Barcode generation for ASP.NET Control to generate, create EAN13 image in ASP.NET applications.
www.OnBarcode.com
Encode ISBN - 10 In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create International Standard Book Number image in ASP.NET applications.
www.OnBarcode.com
Invokes action method
Code 128C Creation In .NET
Using Barcode creation for ASP.NET Control to generate, create Code 128C image in ASP.NET applications.
www.OnBarcode.com
Code 128C Recognizer In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
This test uses the MvcContrib.TestHelper library for an easy test API. To force our controller into an invalid model state, we need to add a model error to ModelState with the AddModelError method B. After setting up our controller, we invoke the Edit action C and examine the result returned D. We assert that a view is rendered with the AssertViewRendered method, which returns a ViewResult object. The ViewName on the ViewResult should be an empty string, signifying that the Edit view is rerendered. Finally, we assert that the Save method on our repository wasn t called. This negative assertion ensures that we don t try to save our Product if it has validation problems. We tested the error condition, and now we need to test our controller in the positive condition that our model didn t have any validation problems. That s shown in listing 26.13.
EAN-13 Decoder In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Data Matrix ECC200 Reader In C#
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Listing 26.13 Testing our controller action when no errors are present
GS1 - 12 Decoder In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Paint Barcode In .NET Framework
Using Barcode creation for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
[Test] public void Edit_should_save_and_redirect_when_no_model_errors_present() { var goodProduct = new Product {Name = "Good value"};
Code 128 Code Set B Generation In None
Using Barcode encoder for Online Control to generate, create Code-128 image in Online applications.
www.OnBarcode.com
Barcode Encoder In Objective-C
Using Barcode generation for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Testing model binders
UPC - 13 Scanner In .NET Framework
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Barcode Maker In Java
Using Barcode maker for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
var repository = MockRepository.GenerateStub<IProductRepository>(); var productsController = new ProductsController(repository); ActionResult result = productsController .Edit(goodProduct); repository.AssertWasCalled(rep => rep.Save(goodProduct)); var redirectResult = Casts result to result as RedirectToRouteResult; correct type Assert.IsNotNull(redirectResult);
Painting Code128 In Java
Using Barcode encoder for Java Control to generate, create Code128 image in Java applications.
www.OnBarcode.com
Reading Barcode In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Invokes action method
Assert.AreEqual(1, redirectResult.RouteValues.Count); Assert.AreEqual("index", redirectResult.RouteValues["action"]); }
Asserts correct results
In this test, we set up our dummy product and controller in a manner similar to the last test, except this time we don t add any model errors to our ModelState. We invoke the Edit action with the product we created B and then verify values on the result. We cast to a RedirectToRouteResult to ensure the type we expect C. Then, we assert that the correct action name is in the route values D. To make both of these tests pass, our action looks like listing 26.14.
Listing 26.14 Implementation of the Edit action
[HttpPost] public ActionResult Edit(Product product) { if (!ModelState.IsValid) { return View(product); } _productRepository.Save(product);
return RedirectToAction("index"); }
In our Edit action, we check for any ModelState errors with the IsValid property B and return a ViewResult with our original Product C. Our Edit view likely will use styling to highlight individual model errors and display a validation error summary. If there are no validation errors, we save the Product and redirect back to the Index action D. With our controller s behavior locked down sufficiently, we can confidently modify our Edit action in the future and know whether our changes break existing functionality. In the next section, we ll examine strategies for testing custom model binders.
26.4 Testing model binders
Custom model binders eliminate much of the boring plumbing that often clutters action methods with code not pertinent to the method s true purpose. But with this powerful tool comes the need for thorough testing. Our infrastructure needs to be rock solid because it will be executing on a large majority of requests.
Testing practices
Testing model binders isn t as straightforward as testing action methods, but it s possible. The amount of testing needed varies depending on what you re doing with your custom model binder. Implementing the IModelBinder interface likely means you ll only need to worry about a single BindModel method and a ModelBindingContext during testing. Inheriting from DefaultModelBinder is a bit more challenging, because any code we add will execute alongside other code that we don t own. We must ensure that any behavior we add works correctly in the context of the other responsibilities of the base DefaultModelBinder class. The DefaultModelBinder class design has extensibility in mind, and key extension points are available through specific method overrides, but we still need to test these methods in the context of an entire binding operation (such as a single BindModel call). In section 14.1, we created a custom model binder that bound entities from a repository. A similar model binder is shown in listing 26.15. If you ve implemented a custom model binder in ASP.NET MVC 1, you ll notice the redesigned value provider API.
Copyright © OnBarcode.com . All rights reserved.