1 / 50

Unit Testing JavaScript

Unit Testing JavaScript. Using Test Driven Development Jon Kruger Email: jon@jonkruger.com Blog: http://jonkruger.com Twitter: JonKruger. Goals of this talk. Explain why you should write unit tests Show you how to write unit tests for JavaScript code

Download Presentation

Unit Testing JavaScript

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit Testing JavaScript Using Test Driven Development Jon Kruger Email: jon@jonkruger.com Blog: http://jonkruger.com Twitter: JonKruger

  2. Goals of this talk • Explain why you should write unit tests • Show you how to write unit tests for JavaScript code • Show you Test Driven Development in action!

  3. I thought this was a .NET user group...

  4. Informal Survey

  5. What is a unit test? • Tests a small unit of functionality • Must run fast • Isolate external dependencies using mocks and stubs

  6. What is not a unit test? • Test that runs against external dependencies (e.g. database, UI, file system) • Test that is slow • Web tests, load tests, performance tests

  7. Why write unit tests? • You will write bad code • You (or someone else) will break your code • Someone else will need to understand your code • No compiler for JavaScript • Peace of mind

  8. What's in it for me? • Proof that your code works • Fewer bugs (both now and in the future) • Peace of mind

  9. Why Test Driven Development? • Design • Edge cases • Separation of concerns • Verification • Documentation • Testable code • When you’re “done” your done

  10. The TDD Lifecycle • Get Specs (features or bugs) • Write tests • Watch ‘emfail • Write code • Watch ‘empass • Repeat!

  11. Your new accountability partner http://PairProgrammingBot.com

  12. ?

  13. JavaScript Unit Testing Frameworks JSUnit J3Unit QUnit JSSpec YUI Test Screw-Unit JSNUnit script.aculo.us unit testing TestCase Crosscheck RhinoUnitjqUnit more?

  14. Official Measure of Relevance

  15. Relevant JavaScript Testing Frameworks QUnit JSSpec YUI Test Screw-Unit

  16. JavaScript class design

  17. JavaScript class design

  18. JavaScript class design

  19. QUnit Basics

  20. QUnit basics – module()

  21. QUnit basics – test()

  22. Untestable JavaScript

  23. Untestable JavaScript Test won’t run unless it’s run from a page that contains these elements.

  24. Testable JavaScript

  25. Untestable JavaScript Testing this method will pop up an alert box!

  26. Testable JavaScript

  27. Untestable JavaScript Unable to test asynchronous AJAX calls.

  28. Testable JavaScript – synchronous AJAX

  29. Testable JavaScript –stop(), start()

  30. ?

  31. Sample project – "MiniSudoku"

  32. Step 1 – Stub out tests

  33. Step 2 – Write tests

  34. Step 2 – Write tests

  35. Step 2 – Write tests

  36. Step 2 – Write tests

  37. Step 3 – Run tests (should fail)

  38. Step 4 – Create classes

  39. Step 4 – Create classes

  40. Step 5 – Run tests (should fail)

  41. Step 6 – Write actual class code

  42. Step 7 – Passing tests!

  43. Step 8 – Repeat! • Get Specs (features or bugs) • Write tests • Watch ‘emfail • Write code • Watch ‘empass • Repeat!

  44. Step 9 – Celebrate!

  45. Hook it up to your .aspx page! • Need to inject UI elements into the tests

  46. What do we have? • I know that my code works • I know what my code is supposed to do - tests are my documentation of my code • I know that my code will continue to work • My classes are well-designed • Tests and good design will mean that it will be easier to make changes to the code • Peace of mind!

  47. Zero Defects • Hold yourself to higher standard • If your code sucks, then stop writing sucky code. • Make quality a priority

  48. ?

  49. Resources • QUnit home page • http://docs.jquery.com/QUnit • Integrating QUnit tests into a continuous integration build • http://tinyurl.com/qunitbuild • MiniSudoku source code • http://jonkruger.com/minisudoku/source.php • My blog • http://jonkruger.com • Send me an email! • jon@jonkruger.com • Twitter • http://twitter.com/jonkruger

More Related