TAILIEUCHUNG - Test Driven JavaScript Development- P3

Test Driven JavaScript Development- P3:This book is about programming JavaScript for the real world, using the techniques and workflow suggested by Test-Driven Development. It is about gaining confidence in your code through test coverage, and gaining the ability to fearlessly refactor and organically evolve your code base. It is about writing modular and testable code. It is about writing JavaScript that works in a wide variety of environments and that doesn’t get in your user’s way. | Test Functions Cases and Suites 13 Setup and Teardown xUnit frameworks usually provide setup and tearDown methods. These are called before and after each test method respectively and allow for centralized setup of test data also known as test fixtures. Let s add the date object as a test fixture using the setup method. Listing shows the augmented testCase function that checks if the test case has setup and tearDown and if so runs them at the appropriate times. Listing Implementing setup and tearDown in testCase function testCase name tests 0 var successful 0 var testCount 0 var hasSetup typeof function var hasTeardown typeof function for var test in tests if Atest .test test continue testCount try if hasSetup tests test output test 0c0 if hasTeardown If the tearDown method throws an error it is considered a test failure so we don t count success until all methods have run successfully successful catch e output test failed c00 Please purchase PDF Split-Merge on to remove this watermark. From the Library of 14 Automated Testing var color successful testCount 0c0 c00 output strong testCount tests testCount - successful failures strong color Using the new setup method we can add an object property to hold the test fixture as shown in Listing Listing Using setup in the strftime test case testCase strftime test setup function new Date 2009 9 2 22 14 45 test format specifier Y function assert Y should return full year Y 2009 . Integration Tests Consider a car manufacturer assembly line. Unit testing corresponds to verifying each individual part of the car the steering wheel wheels electric windows and so on. Integration testing corresponds to verifying that the resulting car works as a whole or that smaller groups of units behave as expected . making sure the wheels turn when the steering wheel is .