Slide 1

Slide 1 text

This slide is intentionally blank Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 2

Slide 2 text

Let's talk about drawing Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 3

Slide 3 text

How to draw Spider-Man Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 4

Slide 4 text

Step 1 Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 5

Slide 5 text

Step 2 Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 6

Slide 6 text

Done! Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 7

Slide 7 text

That's not enough information Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 8

Slide 8 text

In fact, those inital steps seem pointless Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 9

Slide 9 text

You end up with all these unnecssary lines Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 10

Slide 10 text

That just need to be erased Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 11

Slide 11 text

Maybe the person who taught you just wants to sell you erasers Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 12

Slide 12 text

Drawing with lines and ovals Doesn't work Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 13

Slide 13 text

The relationship between steps is complex Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 14

Slide 14 text

Beyond Red/Green/Refactor Testing as a Mindset Noel Rappin, Table XI Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 15

Slide 15 text

We've had a debate about TDD Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 16

Slide 16 text

I learned that I have been a bad TDD advocate Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 17

Slide 17 text

TDD is easy! isn't it? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 18

Slide 18 text

Red Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 19

Slide 19 text

Green Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 20

Slide 20 text

Refactor Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 21

Slide 21 text

Red/Green/Refactor is a slogan not an instruction manual Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 22

Slide 22 text

I may not have made the motivations for TDD clear Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 23

Slide 23 text

I didn't want to scare people away from TDD Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 24

Slide 24 text

Like Object-Oriented design, small problems don't show TDD well Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 25

Slide 25 text

Fully implemented TDD solutions are boring to read about Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 26

Slide 26 text

I may have diminished the value of TDD Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 27

Slide 27 text

Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 28

Slide 28 text

There's still missing information Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 29

Slide 29 text

How do I know what ovals and lines to make? How do I know when each step is done? What if I want to draw Ms. Marvel instead? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 30

Slide 30 text

Why are ovals and lines a good way to draw Spider-Man? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 31

Slide 31 text

How do I decide what to test next? How do I know when I'm done? What do I refactor? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 32

Slide 32 text

Why is test-first a good way to write software? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 33

Slide 33 text

Encourages you to think about the problem In small, focused steps In a way that reinforces specific good practices Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 34

Slide 34 text

What is sometimes called "outside-in" testing is a special case Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 35

Slide 35 text

Write a test Make it pass with an incomplete implementation Write a more focused test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 36

Slide 36 text

Broad tests that catch lots of errors Focused tests that drive design and show where the error is Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 37

Slide 37 text

A lot of problems seem easy to acceptance test But hard to unit test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 38

Slide 38 text

"What to test next" is a design question Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 39

Slide 39 text

Given a sequence of digits 1 to 9, such as [1, 1, 2, 3, 8] 4 The sum is 15 4 At least one digit appears exactly twice 4 No digit appears more than twice 4 Order is irrelevant Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 40

Slide 40 text

code at https://github.com/noelrappin/spec_sequence Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 41

Slide 41 text

Here's what might be the most obivous test: it "knows a valid sequence" do expect(Sequence.new(1, 1, 2, 3, 8)).to be_valid end Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 42

Slide 42 text

And then we would make that test pass... Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 43

Slide 43 text

Wait, How would we make that test pass? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 44

Slide 44 text

We could write the whole algorithm Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 45

Slide 45 text

But that is not letting the test drive the design Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 46

Slide 46 text

Instead, let's take a small step Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 47

Slide 47 text

class Sequence def init(*items) @items = items end def valid? true end end Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 48

Slide 48 text

This makes the test pass Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 49

Slide 49 text

This is OK Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 50

Slide 50 text

Because it is not permanent Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 51

Slide 51 text

It's sometimes called "sliming the test" Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 52

Slide 52 text

Okay, now what? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 53

Slide 53 text

First option: Stay at same abstraction level Write a test case that fails Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 54

Slide 54 text

Broken code it "knows a valid sequence" do expect(Sequence.new(1, 1, 2, 3, 9)).not_to be_valid end Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 55

Slide 55 text

Normally a good option Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 56

Slide 56 text

In this case, though, it leads to another round of "how do we make this pass?" Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 57

Slide 57 text

Rule of thumb: If it's not clear how you might make a test pass, there's probably a smaller test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 58

Slide 58 text

Option 2 Go to a less abstract or smaller piece Write a failing test there Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 59

Slide 59 text

Searching for that smaller test is where design happens Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 60

Slide 60 text

(Sometimes, that smaller test is not worth writing) Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 61

Slide 61 text

A more focused test might be: Part of the job One of the cases An error case Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 62

Slide 62 text

In this case: easy to target the individual clauses (sum of 15, has a pair, etc..) Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 63

Slide 63 text

A smaller test it "can tell if the sum is 15" do expect(Sequence.new(7, 8).to be_correct_sum) end Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 64

Slide 64 text

Why is that better? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 65

Slide 65 text

Because it's small enough that I can write it without thinking too hard Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 66

Slide 66 text

Like so def correct_sum? items.inject(:+) == 15 end Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 67

Slide 67 text

I don't need to slime this test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 68

Slide 68 text

Also, I can negate it easily it "can tell if the sum is not 15" do expect(Sequence.new(7, 8, 1).not_to be_correct_sum) end Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 69

Slide 69 text

TDD should reduce cognitive load Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 70

Slide 70 text

So from here, I can do 4 has a digit pair 4 does not have a digit trio 4 is unique Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 71

Slide 71 text

Once those are in place, the not_valid test is simpler Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 72

Slide 72 text

A plan: Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 73

Slide 73 text

Start with a broad test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 74

Slide 74 text

Find a piece that can be encapsulated Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 75

Slide 75 text

Test a happy-path solution Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 76

Slide 76 text

Try to break it Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 77

Slide 77 text

Where did design come in? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 78

Slide 78 text

I design my functionality as I write the end-to-end test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 79

Slide 79 text

I design my code structure as I break the code up Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 80

Slide 80 text

I design my internal API as I write the happy-path test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 81

Slide 81 text

I refactor to keep my design clean Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 82

Slide 82 text

Sarah Mei Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 83

Slide 83 text

code at https://github.com/noelrappin/spec_sequence Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap

Slide 84

Slide 84 text

Noel Rappin Table XI (tablexi.com) @noelrap 4 http://www.noelrappin.com/trdd 4 http://pragprog.com/book/nrtest2/rails-4-test- prescriptions 4 http://www.patreon.com/noelrap Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap