Slide 1

Slide 1 text

test with :) chen songyong

Slide 2

Slide 2 text

about me

Slide 3

Slide 3 text

about me • Remote worker! • Worked in start-ups, web consultancies , banks and digital agencies! • @aquajach in Twitter & Github

Slide 4

Slide 4 text

test in old days

Slide 5

Slide 5 text

do you ❤️ write test? do you write test?

Slide 6

Slide 6 text

Q & A

Slide 7

Slide 7 text

why you test • Forget to go for test first

Slide 8

Slide 8 text

technical debt "we have not found anything to ease the transition from 'hairball of code with no unit tests' to 'unit-testable code'."

Slide 9

Slide 9 text

trust your mind code

Slide 10

Slide 10 text

my practice integration test controller test model / lib test

Slide 11

Slide 11 text

my practice Test Implementation “Pay” button works controller deals with pay request model/lib makes the transaction views controller model/lib pass pass pass

Slide 12

Slide 12 text

why you test • Forget to write test first! • Too much extra work

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

is it true? •No! v.s. test in rails console

Slide 15

Slide 15 text

why you test • Forget to write test first! • Extra work! • Maintenance is painful

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

why you test • Forget to write test first! • Extra work! • Maintenance is painful! • Running test takes longer and longer

Slide 18

Slide 18 text

easiest way to go • CPU! ! • Data storage! ! • Memory

Slide 19

Slide 19 text

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)

Slide 20

Slide 20 text

Tool / Framework • spork! • zeus! • spring! • Parallel test! • Guard! • … … Ryan Bates’ Railscasts SKIP

Slide 21

Slide 21 text

rspec v.s. rake spec Update: run via bin/rspec

Slide 22

Slide 22 text

database clean strategy • :transaction! • simply rollback - fastest! • Failed at:! • multiple connections! • integration test

Slide 23

Slide 23 text

database clean strategy • :truncation! • TRUNCATE table! • remove all records! • VACUUM table! • index! • Failed at:! • concurrent access to a table

Slide 24

Slide 24 text

database clean strategy • :deletion! • DELETE from table! • scan the table! • less predictable! • Bad at:! • Large tables

Slide 25

Slide 25 text

Deletion Truncation

Slide 26

Slide 26 text

database Clean strategy

Slide 27

Slide 27 text

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’

Slide 28

Slide 28 text

defer garbage collection

Slide 29

Slide 29 text

Original + rspec command + DB clean strategy + refactor Gemfile + delay garbage collection 0 65 130 195 260

Slide 30

Slide 30 text

factory v.s. fixture • Flexibility! ! • Callbacks & validations! ! • Speed

Slide 31

Slide 31 text

! 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

Slide 32

Slide 32 text

not about speed but... • Selective testing! • slow! • focus! • Fail immediately --fail-fast! • Display message immediately! -- format Fuubar! • rspec -p spec

Slide 33

Slide 33 text

(real) Q & A