Testing apps with Rails and JavaScript

  • This is an HTML slide deck.
    • Use the left/right arrow keys to navigate.
    • Press "h" for help.
    • Press "p" for presenter notes.

Presenter Notes

Testing apps with Rails and JavaScript

with Jason Morrison

Presenter Notes

  • Introduce me: used to non-tdd, big QA dept, struggled with tdd, now second nature
  • Hands up: Ruby/Rails? JS? Other? -- Testing?
  • Way too much ground to comprehensively cover TDD practices, techniques, and tools
  • Hoping you find a few interesting things that you can apply right now
  • I'm interested in presenting a broad overview for a range of backgrounds, providing follow-up resources for a range of backgrounds, and starting discussion
  • Stop me to ask questions or drill down

Testing apps with Rails and JavaScript

  • 1: Goals
  • 2: Methods
  • 3: Tools and examples
  • 4: Resources

Presenter Notes

  • 1: What benefit we hope to get
  • 2: From a high level
  • 3: What stack I use
  • 4: Books, blogs, screencasts, cool tools
  • I'll share all the links

1/4: Goals

Presenter Notes

Goals

Cribbed from the thoughtbot playbook

  • Business
  • Code
  • Process

Presenter Notes

Business goals

  • Deliver more value, faster
  • Always ship working software
  • Adapt to change quickly

Presenter Notes

  • We're a consultancy, but this applies to products, too
  • Speed: at the beginning vs at the end

Code goals

  • Readable specs and code
  • Clean public interfaces
  • Decoupled modules

Presenter Notes

Process goals

  • Regression safety net
  • Fearless refactoring

Presenter Notes

  • Hands: Who has made changes or refactorings to a large app? With / without tests? Valuable?

2/4: Methods

Presenter Notes

  • How we approach testing.

How to test

  • Write test first.
  • Red-Green-Refactor cycle.
  • Feedback during development.
  • Catch regressions later.

Presenter Notes

A little terminology

  • Integration test
  • Component test
  • Isolation test
  • Acceptance test
  • End-to-end test
  • Unit test
  • Functional test

Presenter Notes

  • Rails confusions: functional (controller + view), unit (model + db)

Outside-in Development

  • Focus on satisfying user's needs

Presenter Notes

  • Example
  • Two levels
  • Start with integration / acceptance tests, drill in
  • Process of "driving out" or "motivating"
  • Gives us clear understanding of goals and motivation of features
    • Good opportunity to require stakeholders to define who, what and why.

Integration testing

  • Full stack
  • How things fit together

Presenter Notes

  • Exercises entire stack and how different units interact with each other.
  • High level, slower
  • Can expose defects not found in unit tests.
  • 2 concerns: testing from user perspective, testing components integrate correctly

Isolation and component testing

  • Smaller pieces
  • Code design tool

Presenter Notes

  • Specify behavior of smaller units of software.
  • Tight feedback – loop
  • Low level, fast
  • Useful design tool, bad code is hard to test.
  • First client, set up in a clean context. Guides you: coupling, knowledge of other modules, #/collaborators

What to test

  • Things with a high cost or likelihood of failure
    • User stories, main paths
    • Public APIs of your components
    • Complex logic
    • Brittle integration points

Presenter Notes

  • Integration: coarsely-grained user stories, "tracer bullet" / happy cases
  • Public APIs: sanity checks, correctness checks, regression test cases, test-driving to build good interfaces
  • Complex logic: model computations, view/presentation helpers with date/time computation, comparisons, edge cases
  • Brittle integration points (presenters, event bindings)

What not to test

Presenter Notes

What not to test

  • Kent Beck: "I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence. If I don’t typically make a kind of mistake, I don’t test for it."

Presenter Notes

What not to avoid testing

  • Things that are hard to test

Presenter Notes

  • Note: that's different from "time-consuming"

Your optimal approach

Presenter Notes

  • Judgement, intuition of code/benefit
    • Comfort and ability with tools
    • Past experience with the usefulness of an existing suite
  • There are diminishing returns, but the good news with that is: your first efforts should be highly valuable.

3/4: Tools and examples

  • Writing integration tests
  • Writing isolation tests
  • Running your tests

Presenter Notes

Writing integration tests

  • capybara request specs
  • Cucumber?

Presenter Notes

  • Practices Cucumber encourages:
    • thinking outside-in
    • abstract reusable steps, paths, named interface elements
  • You can think outside-in without Cucumber
  • You can write implementation-dependent tests with Cucumber

Writing Rails isolation tests

  • RSpec
  • factory_girl
  • mocha
  • bourne

Presenter Notes

  • Example code of rspec, factory_girl, mocha
    • RSpec, mocha: copycopter/spec/lib/project_cache_job_spec.rb
    • FG: copycopter/spec/models/project_spec.rb

JavaScript tests: choosing your tools

  • From Making JS Unit Testing Work:
    • Ease of writing readable tests
    • Mocking
    • DRY fixtures
    • Continuous integration
    • Support for your favorite debugging tools
    • Support for your backend
    • Cross-browser compatibility

Presenter Notes

  • By Phil Sarin

Presenter Notes

  • Examples of jasmine, jasmine-jquery, and sinon specs
    • jasmine: jasmine_spec.js
    • jasmine-jquery: views/simple_link
    • sinon: http://sinonjs.org/docs/#spies, simple_spy.js, jasmine_spy.js

Interesting JavaScript tests: Timers

  • setTimeout, setInterval, requestAnimationFrame
  • Animations
  • Polling

Presenter Notes

  • examples/jasmine/MockClock
  • poller example

Interesting JavaScript tests: XHR

  • Isolate your client-side specs from the server

Presenter Notes

  • examples/jasmine/fake_xhr
  • Alternatively, just mock $.ajax with Jasmine

Interesting JavaScript tests: Async code

  • Callbacks
  • Timers

Presenter Notes

Running your tests

Presenter Notes

  • Selenium vs Capybara-webkit: stopping and debugging

Continuous integration

Presenter Notes

  • Tddium notes: automatic parallelism, easy setup, and the ability to use parallelism from your local workspace before pushing to a shared CI build. Build artifacts, code coverage, continuous deployment using post-build hooks. Heroku addon. Here's a coupon code you can give out for $50 if they sign up after the 2 week trial: rubyjs2012

4/4: Resources

  • Cool Tools
  • General TDD
  • Rails TDD
  • JavaScript TDD
  • Backbone TDD
  • Writing JavaScript

Presenter Notes

Further reading: General TDD

Presenter Notes

Further reading: Rails TDD

Presenter Notes

Further reading: JavaScript TDD

Presenter Notes

Further reading: Backbone TDD

Presenter Notes

Further reading: Writing JavaScript

Presenter Notes