Unit Testing
the regression shield
Unit Testing, Node.JS style



What is unit testing
“What to test”, not “how to test”
Vows, NodeMock, and CommonSense™
Vows


Use vows properly
Run the test suite runner BEFORE pull requests
Check confessions server AFTER pull request
integration
Test Suite Runner


$ bin/fo test
$ bin/fo test -v (verbose)
$ bin/fo --help (kills nyancats)
Test it out...
Writing a test case

Config Object
Goal:
 Load contents from a config file
 Parse contents and return as a JSON string
 Should handle errors gracefully
See wiki/testing-example1-basic
Writing a test case


What’s the problem here?
 1 - Depends on files
 2 - Hard to setup test environment
 3 - It’s NOT unit testing
Mocks


Control Environment
Run It Anywhere™
Mock objects to the rescue!
See wiki/testing-example2-mock
NodeMock


https://github.com/arunoda/nodemock
Mocking solution made easy
No need to rewrite code for every single test
context
See wiki/testing-example3-nodemock
NodeMock


Mocking objects in test cases are messy
How to re-use mock objects
When to re-use mock objects
When NOT to re-use mock objects
Fixtures

Control environment
“Clean Slate” to run tests on
No external dependencies
Injects dependencies
Used in manufacturing’s testing procedures
See wiki/testing-example4-fixtures
Dependency Injection

WTF is DI?
Inject dependency vs Encapsulated dependency
Constructor injection, Getter/setter injection,
Service Locator, Factory & DI Containers
Dependency Injection = Easier to test
Use FO(‘...’) to auto-construct components
Code Samples


Config File: http://pastebin.com/N65mt2eh
Simple Test: http://pastebin.com/Df7ysdEH
JSON File: http://pastebin.com/Qbt52Cba
With Mock: http://pastebin.com/G0812kxz
NodeMock: http://pastebin.com/swGR379h
Fixture: http://pastebin.com/f4daFf5Z
-Q&A-

Unit testing for 40 square software

  • 1.
  • 2.
    Unit Testing, Node.JSstyle What is unit testing “What to test”, not “how to test” Vows, NodeMock, and CommonSense™
  • 3.
    Vows Use vows properly Runthe test suite runner BEFORE pull requests Check confessions server AFTER pull request integration
  • 4.
    Test Suite Runner $bin/fo test $ bin/fo test -v (verbose) $ bin/fo --help (kills nyancats) Test it out...
  • 5.
    Writing a testcase Config Object Goal: Load contents from a config file Parse contents and return as a JSON string Should handle errors gracefully See wiki/testing-example1-basic
  • 6.
    Writing a testcase What’s the problem here? 1 - Depends on files 2 - Hard to setup test environment 3 - It’s NOT unit testing
  • 7.
    Mocks Control Environment Run ItAnywhere™ Mock objects to the rescue! See wiki/testing-example2-mock
  • 8.
    NodeMock https://github.com/arunoda/nodemock Mocking solution madeeasy No need to rewrite code for every single test context See wiki/testing-example3-nodemock
  • 9.
    NodeMock Mocking objects intest cases are messy How to re-use mock objects When to re-use mock objects When NOT to re-use mock objects
  • 10.
    Fixtures Control environment “Clean Slate”to run tests on No external dependencies Injects dependencies Used in manufacturing’s testing procedures See wiki/testing-example4-fixtures
  • 11.
    Dependency Injection WTF isDI? Inject dependency vs Encapsulated dependency Constructor injection, Getter/setter injection, Service Locator, Factory & DI Containers Dependency Injection = Easier to test Use FO(‘...’) to auto-construct components
  • 12.
    Code Samples Config File:http://pastebin.com/N65mt2eh Simple Test: http://pastebin.com/Df7ysdEH JSON File: http://pastebin.com/Qbt52Cba With Mock: http://pastebin.com/G0812kxz NodeMock: http://pastebin.com/swGR379h Fixture: http://pastebin.com/f4daFf5Z
  • 13.

Editor's Notes