Slide 1

Slide 1 text

................................. ................................. ................................. ................................. ............................ 467 tests, 0 failures, 0 confidence Katrina Owen @kytrinyx JumpstartLab

Slide 2

Slide 2 text

Pointe •◀ .

Slide 3

Slide 3 text

......F...

Slide 4

Slide 4 text

F F F F F F F F F

Slide 5

Slide 5 text

test results not implementation

Slide 6

Slide 6 text

results

Slide 7

Slide 7 text

command query separation . . . . . . . . . . . . . . . . . . ✂

Slide 8

Slide 8 text

returns a value, has no side effects query

Slide 9

Slide 9 text

has side effects, returns nil command

Slide 10

Slide 10 text

. . . . . . . . . . . . . . . . . ✂ command query

Slide 11

Slide 11 text

query @waiter.menu

Slide 12

Slide 12 text

command @bill.add(steak)

Slide 13

Slide 13 text

both ... it’s complicated

Slide 14

Slide 14 text

responsibility and collaboration

Slide 15

Slide 15 text

mock expectation

Slide 16

Slide 16 text

mock stub . . . . . . . . . . . . . . . . . ✂ assertion dumb value

Slide 17

Slide 17 text

who is responsible?

Slide 18

Slide 18 text

sender :message receiver outgoing incoming

Slide 19

Slide 19 text

incoming assert the state of the system

Slide 20

Slide 20 text

outgoing assert collaboration

Slide 21

Slide 21 text

rules of thumb

Slide 22

Slide 22 text

Query Incoming ?

Slide 23

Slide 23 text

Query Incoming assert return value

Slide 24

Slide 24 text

Query Command Incoming assert return value ?

Slide 25

Slide 25 text

Query Command Incoming assert return value assert side effects

Slide 26

Slide 26 text

Query Command Incoming Outgoing assert return value assert side effects ?

Slide 27

Slide 27 text

Query Command Incoming Outgoing assert return value assert side effects do not assert

Slide 28

Slide 28 text

Query Command Incoming Outgoing assert return value assert side effects do not assert ?

Slide 29

Slide 29 text

Query Command Incoming Outgoing assert return value assert side effects do not assert assert message sent

Slide 30

Slide 30 text

Query Command Incoming Private Outgoing assert return value assert side effects ? ? do not assert assert message sent

Slide 31

Slide 31 text

Query Command Incoming Private Outgoing assert return value assert side effects do not assert do not assert do not assert assert message sent

Slide 32

Slide 32 text

Query Command Incoming Private Outgoing assert return value assert side effects do not assert do not assert do not assert assert message sent

Slide 33

Slide 33 text

1incoming query assert return value

Slide 34

Slide 34 text

1incoming query assert return value assert_equal "...", @waiter.menu

Slide 35

Slide 35 text

assert direct, local side effects 2 incoming command

Slide 36

Slide 36 text

assert direct, local side effects 2 incoming command bill.add(steak)

Slide 37

Slide 37 text

assert direct, local side effects 2 bill.add(steak) assert_equal 32.90, bill.total incoming command

Slide 38

Slide 38 text

3outgoing query do not assert

Slide 39

Slide 39 text

3outgoing query do not assert chef.todays_special

Slide 40

Slide 40 text

assert message send 4 outgoing command

Slide 41

Slide 41 text

assert message send kitchen.expect(:order_up) 4 outgoing command

Slide 42

Slide 42 text

assert message send kitchen.expect(:order_up) waiter.i_would_like(:steak) 4 outgoing command

Slide 43

Slide 43 text

assert message send kitchen.expect(:order_up) waiter.i_would_like(:steak) kitchen.verify 4 outgoing command

Slide 44

Slide 44 text

assert message send 4 outgoing command kitchen.should_receive(:order_up) waiter.i_would_like(:steak)

Slide 45

Slide 45 text

5private message do not assert

Slide 46

Slide 46 text

5private message do not assert note_to_self(stuff)

Slide 47

Slide 47 text

unit test

Slide 48

Slide 48 text

unit test return values +

Slide 49

Slide 49 text

unit test return values + side effects +

Slide 50

Slide 50 text

unit test return values + side effects + critical interations +

Slide 51

Slide 51 text

unit test return values + side effects + critical interations + implementation details -

Slide 52

Slide 52 text

meet practice theory

Slide 53

Slide 53 text

github.com/it-agile codersdojo_client

Slide 54

Slide 54 text

@kata.generate 'ruby.rspec', 'prime'

Slide 55

Slide 55 text

code kata small coding exercise

Slide 56

Slide 56 text

@kata.generate 'ruby.rspec', 'prime'

Slide 57

Slide 57 text

@kata.generate 'ruby.rspec', 'prime'

Slide 58

Slide 58 text

@kata.generate 'ruby.rspec', 'prime'

Slide 59

Slide 59 text

@kata.generate 'ruby.rspec', 'prime'

Slide 60

Slide 60 text

class Prime def foo 'fixme' end end

Slide 61

Slide 61 text

class Prime def foo 'fixme' end end describe Prime do let(:prime) { Prime.new } its(:foo) { should == 'bar' } end

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

limit work in progress

Slide 64

Slide 64 text

Query Command Incoming Private Outgoing ✓

Slide 65

Slide 65 text

incoming command assert direct, local side effects

Slide 66

Slide 66 text

describe Scaffolder do it "generates the files and directories" it "replaces placeholders" end

Slide 67

Slide 67 text

describe Scaffolder do before (:each) do @shell = mock @shell.should_receive(:shell_extension).any_number_of_times.and_return "cmd" @shell.should_receive(:remove_command_name).any_number_of_times.and_return "del" @shell.should_receive(:path_separator).any_number_of_times.and_return ";" @shell.should_receive(:dir_separator).any_number_of_times.and_return "/" Scaffolder.should_receive(:current_file).any_number_of_times.and_return("aDir/lib/aFile.rb") @scaffolder = Scaffolder.new @shell end it "generates the files and directories" do @shell.should_receive(:real_dir_entries).with("aDir/templates/a.template").and_return ["a", "b"] @shell.should_receive(:cp_r).with "aDir/templates/a.template/a", "." @shell.should_receive(:file?).with("a").and_return false @shell.should_receive(:cp_r).with "aDir/templates/a.template/b", "." @shell.should_receive(:file?).with("b").and_return fals @scaffolder.scaffold "a.template", 'myKata' end it "replaces placeholders" do @shell.should_receive(:real_dir_entries).with("aDir/templates/a.template").and_return ["a", "README", "run-once.sh", "run-endless.sh"] @shell.should_receive(:cp_r).with "aDir/templates/a.template/a", "." @shell.should_receive(:file?).with("a").and_return false @shell.should_receive(:cp_r).with "aDir/templates/a.template/README", "." @shell.should_receive(:file?).with("README").and_return true @shell.should_receive(:read_file).with("README").and_return '%rm% %kata_file%\nb.%sh%\nc%:%d' @shell.should_receive(:write_file).with "README", 'del myKata\nb.cmd\nc;d' @shell.should_receive(:cp_r).with "aDir/templates/a.template/run-once.sh", "." @shell.should_receive(:file?).with("run-once.sh").and_return true @shell.should_receive(:read_file).with("run-once.sh").and_return "%rm% a\nb.%sh%\nc%:%d" @shell.should_receive(:write_file).with "run-once.sh", "del a\nb.cmd\nc;d" @shell.should_receive(:cp_r).with "aDir/templates/a.template/run-endless.sh", "." @shell.should_receive(:file?).with("run-endless.sh").and_return true @shell.should_receive(:read_file).with("run-endless.sh").and_return "%rm% a\nb.%sh%\nc%:%d" @shell.should_receive(:write_file).with "run-endless.sh", "del a\nb.cmd\nc;d" @scaffolder.scaffold "a.template", 'myKata' end end

Slide 68

Slide 68 text

describe Scaffolder do it "generates the files and directories" do @shell.should_receive(:real_dir_entries).with("aDir/templates/a.template").and @shell.should_receive(:cp_r).with "aDir/templates/a.template/a", "." @shell.should_receive(:file?).with("a").and_return false @shell.should_receive(:cp_r).with "aDir/templates/a.template/b", "." @shell.should_receive(:file?).with("b").and_return false @scaffolder.generate "a.template", 'myKata' end end

Slide 69

Slide 69 text

describe Scaffolder do it "replaces placeholders" do @shell.should_receive(:real_dir_entries).with("aDir/templates/a.template").and @shell.should_receive(:cp_r).with "aDir/templates/a.template/a", "." @shell.should_receive(:file?).with("a").and_return false @shell.should_receive(:cp_r).with "aDir/templates/a.template/README", "." @shell.should_receive(:file?).with("README").and_return true @shell.should_receive(:read_file).with("README").and_return '%rm% %kata_file%\ @shell.should_receive(:write_file).with "README", 'del myKata\nb.cmd\nc;d' @shell.should_receive(:cp_r).with "aDir/templates/a.template/run-once.sh", "." @shell.should_receive(:file?).with("run-once.sh").and_return true @shell.should_receive(:read_file).with("run-once.sh").and_return "%rm% a\nb.%s @shell.should_receive(:write_file).with "run-once.sh", "del a\nb.cmd\nc;d" @shell.should_receive(:cp_r).with "aDir/templates/a.template/run-endless.sh", @shell.should_receive(:file?).with("run-endless.sh").and_return true @shell.should_receive(:read_file).with("run-endless.sh").and_return "%rm% a\nb @shell.should_receive(:write_file).with "run-endless.sh", "del a\nb.cmd\nc;d" @scaffolder.generate "a.template", 'myKata' end end

Slide 70

Slide 70 text

describe Scaffolder do before(:each) do @shell = mock @shell.should_receive(:shell_extension).any_number_of_times.and_return "cmd" @shell.should_receive(:remove_command_name).any_number_of_times.and_return "de @shell.should_receive(:path_separator).any_number_of_times.and_return ";" @shell.should_receive(:dir_separator).any_number_of_times.and_return "/" Scaffolder.should_receive(:current_file).any_number_of_times.and_return("aDir/ @scaffolder = Scaffolder.new @shell end end

Slide 71

Slide 71 text

ZOM G ......F...

Slide 72

Slide 72 text

<3

Slide 73

Slide 73 text

test all the things

Slide 74

Slide 74 text

30 assertions

Slide 75

Slide 75 text

30 assertions @shell.should_receive(:x)

Slide 76

Slide 76 text

30 assertions @shell.should_receive(:x) .with(y)

Slide 77

Slide 77 text

30 assertions @shell.should_receive(:x) .with(y) .and_return z

Slide 78

Slide 78 text

......... ......... ......... .........

Slide 79

Slide 79 text

unit tests 30 assertions 20 assertions 10 assertions 0 assertions

Slide 80

Slide 80 text

unit tests testing what, exactly?

Slide 81

Slide 81 text

@kata.generate README prime.rb run.sh ... ?

Slide 82

Slide 82 text

@kata.generate README prime.rb run.sh ...

Slide 83

Slide 83 text

magic happens here

Slide 84

Slide 84 text

@shell.should_receive(:real_dir_entries).with("aDir/ @shell.should_receive(:cp_r).with "aDir/templates/a. @shell.should_receive(:file?).with("a").and_return f @shell.should_receive(:cp_r).with "aDir/templates/a. @shell.should_receive(:file?).with("README").and_ret @shell.should_receive(:read_file).with("README").and @shell.should_receive(:write_file).with "README", 'd @shell.should_receive(:cp_r).with "aDir/templates/a. @shell.should_receive(:file?).with("run-once.sh").an @shell.should_receive(:read_file).with("run-once.sh" @shell.should_receive(:write_file).with "run-once.sh @shell.should_receive(:cp_r).with "aDir/templates/a. @shell.should_receive(:file?).with("run-endless.sh") @shell.should_receive(:read_file).with("run-endless. @shell.should_receive(:write_file).with "run-endless

Slide 85

Slide 85 text

delegation vs micro-management

Slide 86

Slide 86 text

unit tests have conceded defeat

Slide 87

Slide 87 text

describe Scaffolder do before (:each) do @shell = mock @shell.should_receive(:shell_extension).any_number_of_times.and_return "cmd" @shell.should_receive(:remove_command_name).any_number_of_times.and_return "del" @shell.should_receive(:path_separator).any_number_of_times.and_return ";" @shell.should_receive(:dir_separator).any_number_of_times.and_return "/" Scaffolder.should_receive(:current_file).any_number_of_times.and_return("aDir/lib/aFile.rb") @scaffolder = Scaffolder.new @shell end it "generates the files and directories" do @shell.should_receive(:real_dir_entries).with("aDir/templates/a.template").and_return ["a", "b"] @shell.should_receive(:cp_r).with "aDir/templates/a.template/a", "." @shell.should_receive(:file?).with("a").and_return false @shell.should_receive(:cp_r).with "aDir/templates/a.template/b", "." @shell.should_receive(:file?).with("b").and_return false @scaffolder.scaffold "a.template", 'myKata' end it "replaces placeholders" do @shell.should_receive(:real_dir_entries).with("aDir/templates/a.template").and_return ["a", "README", "run-once.sh", "run-endless.sh"] @shell.should_receive(:cp_r).with "aDir/templates/a.template/a", "." @shell.should_receive(:file?).with("a").and_return false @shell.should_receive(:cp_r).with "aDir/templates/a.template/README", "." @shell.should_receive(:file?).with("README").and_return true @shell.should_receive(:read_file).with("README").and_return '%rm% %kata_file%\nb.%sh%\nc%:%d' @shell.should_receive(:write_file).with "README", 'del myKata\nb.cmd\nc;d' @shell.should_receive(:cp_r).with "aDir/templates/a.template/run-once.sh", "." @shell.should_receive(:file?).with("run-once.sh").and_return true @shell.should_receive(:read_file).with("run-once.sh").and_return "%rm% a\nb.%sh%\nc%:%d" @shell.should_receive(:write_file).with "run-once.sh", "del a\nb.cmd\nc;d" @shell.should_receive(:cp_r).with "aDir/templates/a.template/run-endless.sh", "." @shell.should_receive(:file?).with("run-endless.sh").and_return true @shell.should_receive(:read_file).with("run-endless.sh").and_return "%rm% a\nb.%sh%\nc%:%d" @shell.should_receive(:write_file).with "run-endless.sh", "del a\nb.cmd\nc;d" @scaffolder.scaffold "a.template", 'myKata' end end

Slide 88

Slide 88 text

unit tests for the generated kata files

Slide 89

Slide 89 text

integration tests for the generated kata files

Slide 90

Slide 90 text

characterization

Slide 91

Slide 91 text

characterization run the code

Slide 92

Slide 92 text

characterization run the code make assertions about whatever happens

Slide 93

Slide 93 text

describe Scaffolder do end

Slide 94

Slide 94 text

describe Scaffolder do it "works" do scaffolder.generate('ruby.rspec', 'prime') end end

Slide 95

Slide 95 text

describe Scaffolder do let(:scaffolder) { } it "works" do scaffolder.generate('ruby.rspec', 'prime') end end

Slide 96

Slide 96 text

describe Scaffolder do let(:scaffolder) { Scaffolder.new } it "works" do scaffolder.generate('ruby.rspec', 'prime') end end

Slide 97

Slide 97 text

describe Scaffolder do let(:scaffolder) { Scaffolder.new(shell) } it "works" do scaffolder.generate('ruby.rspec', 'prime') end end

Slide 98

Slide 98 text

describe Scaffolder do let(:shell) { ShellWrapper.new } let(:scaffolder) { Scaffolder.new(shell) } it "works" do scaffolder.scaffold('ruby.rspec', 'prime') end end

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

$ git status

Slide 101

Slide 101 text

$ git status # Untracked files: # #!.meta #!README #!prime.rb #!run-endless.sh #!run-once.sh

Slide 102

Slide 102 text

describe Scaffolder do let(:files) do [ '.meta', 'prime.rb', 'README', 'run-endless.sh', 'run-once.sh' ] end it "works"... end

Slide 103

Slide 103 text

describe Scaffolder do it "works" do scaffolder.scaffold('ruby.rspec', 'prime') expect(File.read('.meta')).to eq('...') expect(File.read('README')).to eq('...') # etc end end

Slide 104

Slide 104 text

describe Scaffolder do after(:each) do files.each do |file| FileUtils.rm_f(file) end end it "works" do # ... end end

Slide 105

Slide 105 text

Finished in 0.00632 seconds 1 example, 0 failures .

Slide 106

Slide 106 text

I’m no genius and so can you

Slide 107

Slide 107 text

reality vs fantasy

Slide 108

Slide 108 text

red flag time to refactor

Slide 109

Slide 109 text

the secret fate of a characterization test

Slide 110

Slide 110 text

confidence to refactor

Slide 111

Slide 111 text

decouple template source and kata destination

Slide 112

Slide 112 text

simplify the algorithm

Slide 113

Slide 113 text

spot the abstractions

Slide 114

Slide 114 text

a KataFile KataFile = Struct.new(:name, :contents)

Slide 115

Slide 115 text

kata a Translator template

Slide 116

Slide 116 text

a KataStore ./templates/ruby.rspec/* [kata_file1, kata_file2, ...] [kata_file1, kata_file2, ...] /tmp/*

Slide 117

Slide 117 text

or something like that

Slide 118

Slide 118 text

theory vs practice

Slide 119

Slide 119 text

clarity and chaos

Slide 120

Slide 120 text

Query Command Incoming Private Outgoing assert return value assert side effects do not assert do not assert do not assert assert message sent

Slide 121

Slide 121 text

F F F F F F F thank you <3 <3 <3 ......... c

Slide 122

Slide 122 text

Practical Object Oriented Design in Ruby Sandi Metz The Magic Tricks of Testing Sandi Metz Working Effectively with Legacy Code Michael Feathers CodersDojo Client FreeBSD License, (c) The CodersDojo Team Limited Red Joseph Wilk Fonts & Colors Damon Davison