50
Using Test Driven Development Jon Kruger Email: [email protected] Blog: http://jonkruger.com Twitter: JonKruger Unit Testing JavaScript

Unit Testing JavaScript

  • Upload
    peers

  • View
    41

  • Download
    0

Embed Size (px)

DESCRIPTION

Unit Testing JavaScript. Using Test Driven Development Jon Kruger Email: [email protected] 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 - PowerPoint PPT Presentation

Citation preview

Page 1: Unit Testing JavaScript

Using Test Driven Development

Jon KrugerEmail: [email protected]: http://jonkruger.com

Twitter: JonKruger

Unit Testing JavaScript

Page 2: Unit Testing JavaScript

Explain why you should write unit testsShow you how to write unit tests for

JavaScript codeShow you Test Driven Development in

action!

Goals of this talk

Page 3: Unit Testing JavaScript

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

Page 4: Unit Testing JavaScript

Informal Survey

Page 5: Unit Testing JavaScript

Tests a small unit of functionalityMust run fastIsolate external dependencies using

mocks and stubs

What is a unit test?

Page 6: Unit Testing JavaScript

Test that runs against external dependencies (e.g. database, UI, file system)

Test that is slowWeb tests, load tests, performance tests

What is not a unit test?

Page 7: Unit Testing JavaScript

You will write bad codeYou (or someone else) will break your

codeSomeone else will need to understand

your codeNo compiler for JavaScriptPeace of mind

Why write unit tests?

Page 8: Unit Testing JavaScript

Proof that your code worksFewer bugs (both now and in the future)Peace of mind

What's in it for me?

Page 9: Unit Testing JavaScript
Page 10: Unit Testing JavaScript

1. Design Edge cases Separation of concerns

2. Verification3. Documentation4. Testable code5. When you’re “done” your done

Why Test Driven Development?

Page 11: Unit Testing JavaScript

The TDD Lifecycle

Get Specs (features or bugs)

Write tests

Watch ‘em fail

Write code

Watch ‘em pass

Repeat!

Page 12: Unit Testing JavaScript

http://PairProgrammingBot.com

Your new accountability partner

Page 13: Unit Testing JavaScript

?

Page 14: Unit Testing JavaScript

JavaScript Unit Testing FrameworksJSUnit J3UnitQUnit JSSpecYUI Test Screw-UnitJSNUnit script.aculo.us unit

testingTestCase Crosscheck

RhinoUnit jqUnitmore?

Page 15: Unit Testing JavaScript

Official Measure of Relevance

Page 16: Unit Testing JavaScript

Relevant JavaScript Testing Frameworks

QUnitJSSpec

YUI TestScrew-Unit

Page 17: Unit Testing JavaScript

JavaScript class design

Page 18: Unit Testing JavaScript

JavaScript class design

Page 19: Unit Testing JavaScript

JavaScript class design

Page 20: Unit Testing JavaScript

QUnit Basics

Page 21: Unit Testing JavaScript

QUnit basics – module()

Page 22: Unit Testing JavaScript

QUnit basics – test()

Page 23: Unit Testing JavaScript

Untestable JavaScript

Page 24: Unit Testing JavaScript

Untestable JavaScript

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

Page 25: Unit Testing JavaScript

Testable JavaScript

Page 26: Unit Testing JavaScript

Untestable JavaScript

Testing this method will pop up an alert box!

Page 27: Unit Testing JavaScript

Testable JavaScript

Page 28: Unit Testing JavaScript

Untestable JavaScript

Unable to test asynchronous AJAX calls.

Page 29: Unit Testing JavaScript

Testable JavaScript – synchronous AJAX

Page 30: Unit Testing JavaScript

Testable JavaScript –stop(), start()

Page 31: Unit Testing JavaScript

?

Page 32: Unit Testing JavaScript

Sample project – "MiniSudoku"

Page 33: Unit Testing JavaScript

Step 1 – Stub out tests

Page 34: Unit Testing JavaScript

Step 2 – Write tests

Page 35: Unit Testing JavaScript

Step 2 – Write tests

Page 36: Unit Testing JavaScript

Step 2 – Write tests

Page 37: Unit Testing JavaScript

Step 2 – Write tests

Page 38: Unit Testing JavaScript

Step 3 – Run tests (should fail)

Page 39: Unit Testing JavaScript

Step 4 – Create classes

Page 40: Unit Testing JavaScript

Step 4 – Create classes

Page 41: Unit Testing JavaScript

Step 5 – Run tests (should fail)

Page 42: Unit Testing JavaScript

Step 6 – Write actual class code

Page 43: Unit Testing JavaScript

Step 7 – Passing tests!

Page 44: Unit Testing JavaScript

Step 8 – Repeat!

Get Specs (features or bugs)

Write tests

Watch ‘em fail

Write code

Watch ‘em pass

Repeat!

Page 45: Unit Testing JavaScript

Step 9 – Celebrate!

Page 46: Unit Testing JavaScript

Need to inject UI elements into the tests

Hook it up to your .aspx page!

Page 47: Unit Testing JavaScript

I know that my code worksI know what my code is supposed to do - tests

are my documentation of my codeI know that my code will continue to workMy classes are well-designed Tests and good design will mean that it will

be easier to make changes to the codePeace of mind!

What do we have?

Page 48: Unit Testing JavaScript

Hold yourself to higher standardIf your code sucks, then stop writing sucky

code.Make quality a priority

Zero Defects

Page 49: Unit Testing JavaScript

?

Page 50: Unit Testing JavaScript

QUnit home pagehttp://docs.jquery.com/QUnit

Integrating QUnit tests into a continuous integration build http://tinyurl.com/qunitbuild

MiniSudoku source codehttp://jonkruger.com/minisudoku/source.php

My bloghttp://jonkruger.com

Send me an email! [email protected]

Twitterhttp://twitter.com/jonkruger

Resources