DESIGN FOR EASIER TESTING in Font

Creation Code39 in Font DESIGN FOR EASIER TESTING

CHAPTER 10 DESIGN FOR EASIER TESTING
USS Code 39 Encoder In None
Using Barcode creation for Font Control to generate, create Code 3 of 9 image in Font applications.
www.OnBarcode.com
Code128 Creator In None
Using Barcode printer for Font Control to generate, create Code 128 Code Set C image in Font applications.
www.OnBarcode.com
Figure 10 7. A Flex UI matching the Quote Hotel Price use case. Compare with the guessed-at UI from 9. Another problem with the Flex code from 9 was that the form validation logic was closely tied into the UI markup. There was little or no OO encapsulation going on. To fix this, we could create a PriceSearch class and give it properties matching the form. Here s the beginning of such a class, before any behavior is added: public class PriceSearch { private var numNights: int; private var hotelID: String; } But if you check the screenshot in Figure 10 7, it s obvious that this would have been created from a Hotel object so why not simply add a quotePrice() method to Hotel Here s how it could look: public class Hotel { private var id: String; public var pricePerNight: Number; // . . .
Print Barcode In None
Using Barcode creator for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Barcode Creation In None
Using Barcode creator for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
CHAPTER 10 DESIGN FOR EASIER TESTING
Generate UPC-A In None
Using Barcode generation for Font Control to generate, create UPC-A Supplement 2 image in Font applications.
www.OnBarcode.com
EAN / UCC - 13 Generation In None
Using Barcode generator for Font Control to generate, create UPC - 13 image in Font applications.
www.OnBarcode.com
public function quotePrice(numNights: int): Number { if (isNaN(Number(nights))) { throw new ValidationError("Please enter a valid number of nights."); } var numNights: int = parseInt(nights); if (numNights < 0) { throw new ValidationError("Please enter a positive integer."); } // Call the remote Java service: return PriceService.fetchQuote(this, numNights);
Generate Data Matrix ECC200 In None
Using Barcode generation for Font Control to generate, create Data Matrix image in Font applications.
www.OnBarcode.com
Print Code 93 In None
Using Barcode creator for Font Control to generate, create USS Code 93, USS 93 image in Font applications.
www.OnBarcode.com
Notice how the validation is now being done in this non-UI class, not in the UI mark-up. The quotePrice() function throws a ValidationError if the input fails validation. We ought to catch this in the UI button-click event handler, and show an appropriate dialog box. Here are the relevant components from the UI mark-up: <s:TextInput id="txtNights" /> <s:Button label="Calculate Price" click="btnCalculate_clickHandler(event)" id="btnCalculate"/> <s:Label text="{totalCostOfStay}" /> <s:Label text="${hotel.pricePerNight}" /> And here s the ActionScript to go with it notice how both hotel and totalCostOfStay are bindable: import mx.controls.Alert; [Bindable] private var hotel: Hotel = new Hotel(); [Bindable] private var totalCostOfStay: String; protected function btnCalculate_clickHandler(event:MouseEvent):void { try { totalCostOfStay = "$" + hotel.quotePrice(txtNights.text); } catch (error: ValidationError) { Alert.show(error.message, "Validation Error"); } }
Make Code-39 In Visual C#.NET
Using Barcode maker for .NET Control to generate, create Code 3 of 9 image in .NET applications.
www.OnBarcode.com
Code39 Creator In None
Using Barcode generator for Software Control to generate, create USS Code 39 image in Software applications.
www.OnBarcode.com
CHAPTER 10 DESIGN FOR EASIER TESTING
Drawing Code 128C In None
Using Barcode creator for Office Excel Control to generate, create Code 128 Code Set C image in Excel applications.
www.OnBarcode.com
Creating EAN-13 In Visual Studio .NET
Using Barcode creation for VS .NET Control to generate, create EAN-13 image in .NET framework applications.
www.OnBarcode.com
Note If you look at the ActionScript code, the totalCostOfStay variable is tagged as bindable. So when its value is changed, the Total Cost label will be automatically redrawn with the new value. The Hotel object is also bindable, because we re using its pricePerNight value in the price per night label.
Quick Response Code Creator In Java
Using Barcode drawer for Java Control to generate, create Denso QR Bar Code image in Java applications.
www.OnBarcode.com
UPC-A Supplement 2 Encoder In Objective-C
Using Barcode encoder for iPhone Control to generate, create UPC A image in iPhone applications.
www.OnBarcode.com
In the button click handler, the hotel s quotePrice() function is called with the text value of txtNights. If any validation fails (non-numeric or negative value entered), the UI code catches the ValidationError and displays an alert dialog see Figure 10 8.
Decode UPC-A In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Painting Barcode In None
Using Barcode drawer for Microsoft Excel Control to generate, create Barcode image in Excel applications.
www.OnBarcode.com
Figure 10 8. Running a unit test is one thing, but good old visual inspection seeing exactly what the user sees is important, too. Adding a unit test for the validation code should now be pretty straightforward, as Hotel is a nonUI class. We can further split out the validation from the quotePrice() function to make it easier still. The two functions in Hotel are now the following: public function quotePrice(nights: String): Number { validateNumNights(nights); var numNights: int = parseInt(nights);
Code-39 Drawer In None
Using Barcode generation for Software Control to generate, create Code 3/9 image in Software applications.
www.OnBarcode.com
Making QR Code In Visual C#
Using Barcode creation for .NET Control to generate, create QR Code 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 10 DESIGN FOR EASIER TESTING
Barcode Maker In None
Using Barcode maker for Word Control to generate, create Barcode image in Microsoft Word applications.
www.OnBarcode.com
Print QR Code In VS .NET
Using Barcode drawer for Reporting Service Control to generate, create QR Code ISO/IEC18004 image in Reporting Service applications.
www.OnBarcode.com
return PriceService.fetchQuote(this, numNights);
public function validateNumNights(nights: String) { if (isNaN(Number(nights))) { throw new ValidationError("Please enter a valid number of nights."); } var numNights: int = parseInt(nights); if (numNights < 0) { throw new ValidationError("Please enter a positive integer."); } Here s the FlexUnit 4 unit test code for Hotel.validateNumNights(): public class HotelTest { private var hotel : Hotel; [Before] public function setUp(): void { hotel = new Hotel(); } [After] public function tearDown(): void { hotel = null; } [Test] public function testValidateNumNights():void { // Expect no ValidationError to be thrown: hotel.validateNumNights("1"); } [Test(expects="ValidationError")] public function testValidateNumNights_Negative():void { hotel.validateNumNights("-1"); } [Test(expects="ValidationError")] public function testValidateNumNights_NonNumeric():void { hotel.validateNumNights("xyz"); }
CHAPTER 10 DESIGN FOR EASIER TESTING
The three test functions are pretty straightforward. The first test is the basic course : a valid number is entered, so we don t expect a ValidationError to be thrown. The other two functions test for a negative number and a non-numeric number respectively: for both of these, we expect a ValidationError, and the test fails if we don t get one. Figure 10 9 shows the warm and snuggly green bar when we run the test class in Flash Builder.
Figure 10 9. Green bar of Shangri oh, you get the idea The main point here is to compare this code with the corresponding antipattern #4 in 9: that version would have been impossible to unit-test, whereas with the validation code separated into a non-UI class, it s now very straightforward.
Copyright © OnBarcode.com . All rights reserved.