TAILIEUCHUNG - Test Driven JavaScript Development- P19

Test Driven JavaScript Development- P19: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. | The Controller 353 Unfortunately this doesn t play out exactly as planned. The previous test which also calls post is now attempting to call addMessage on chatRoom which is undefined in that test. We can fix the issue by moving the chatRoom stub into setUp as Listing does. Listing Sharing the chatRoom stub function controllerSetUp . addMessage stub All the tests go back to a soothing green and we can turn our attention to the duplicated logic we just introduced in the second test. In particular both tests simulates sending a request with a body. We can simplify the tests considerably by extracting this logic into the setup. Listing shows the updated tests. Listing Cleaning up post tests function controllerSetUp . function data var str encodeURI data data 0 2 data 2 end testCase exports . should parse request body as JSON function test var data data user cjno message hi stub data data 0 data . Please purchase PDF Split-Merge on to remove this watermark. From the Library of 354 Server-Side JavaScript with The cleaned up tests certainly are a lot easier to follow and with the sendRequest helper method writing new tests that make requests will be easier as well. All tests pass and we can move on. Malicious Data Notice that we are currently accepting messages completely unfiltered. This can lead to all kinds of scary situations for instance consider the effects of the request in Listing Listing Malicious request topic message data user cjno message script http hacked script Before deploying an application like the one we are currently building we should take care to not blindly accept any end user .