Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Test with :)

aquajach
October 26, 2013

Test with :)

The slides for Rubyconf China 2013 with animation off

aquajach

October 26, 2013
Tweet

More Decks by aquajach

Other Decks in Technology

Transcript

  1. about me • Remote worker! • Worked in start-ups, web

    consultancies , banks and digital agencies! • @aquajach in Twitter & Github
  2. technical debt "we have not found anything to ease the

    transition from 'hairball of code with no unit tests' to 'unit-testable code'."
  3. my practice Test Implementation “Pay” button works controller deals with

    pay request model/lib makes the transaction views controller model/lib pass pass pass
  4. is it true? Writing test adds 15-35% development time in

    return for a 40-90% reduction in defect density on otherwise like-for-like projects. - A paper in ESE •YES
  5. why you test • Forget to write test first! •

    Extra work! • Maintenance is painful
  6. it 'Sign Up should have title' do! visit '/tc/'! page.find('div#sign-up-modal').should

    have_css(‘#modalHeader.h5-sign-up-header’, :text => “Apple Now”) ! end
  7. why you test • Forget to write test first! •

    Extra work! • Maintenance is painful! • Running test takes longer and longer
  8. test suite example • ruby 2.0.0 / rails 3.2.13 /

    rspec-rails ~>2.0 / PostgreSQL /Factory Girl / Database cleaner! • 370+ mixed with unit test, integration test (w/ capybara inc. JavaScript)! • No remote request (w/ VCR)
  9. Tool / Framework • spork! • zeus! • spring! •

    Parallel test! • Guard! • … … Ryan Bates’ Railscasts SKIP
  10. database clean strategy • :transaction! • simply rollback - fastest!

    • Failed at:! • multiple connections! • integration test
  11. database clean strategy • :truncation! • TRUNCATE table! • remove

    all records! • VACUUM table! • index! • Failed at:! • concurrent access to a table
  12. database clean strategy • :deletion! • DELETE from table! •

    scan the table! • less predictable! • Bad at:! • Large tables
  13. refactor gemfile • stop loading gems not used in the

    code! • e.g. gem ‘unicorn’, require: false! • specify dependencies for test environment! • ‘jquery-rails’, ‘will_paginate’, ‘exception_notification’
  14. Original + rspec command + DB clean strategy + refactor

    Gemfile + delay garbage collection 0 65 130 195 260
  15. ! best practice • create(:user) v.s. build(:user) v.s. build_stubbed(:user)! •

    require ‘spec_helper’ v.s. require ‘lib_class.rb’! • mock/stub or not to mock/stub! • let v.s. let! v.s. before block
  16. not about speed but... • Selective testing! • slow! •

    focus! • Fail immediately --fail-fast! • Display message immediately! -- format Fuubar! • rspec -p spec