This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
What we talk aboutWhen we talk about
TestingNoel Rappin, Table XI
Or... Beyond
RedGreen
Refactor
Do you
test?
Do you feel
guiltyabout not testing
more
Why?
No
truetester
TDDis
hard
There arespecific skills
How Does TDD Work?
RedGreen
Refactor
But...Red, green, refactor is a better
slogan than an instruction manual
What do I test next?How do I split my
problem into testable components?
If you TDD in blocks that are too big TDD is
a real pain
Sample problem
Find all the unique sequences of digits 1 to 9, such as [1, 1, 2, 3, 8]
• The sum is 15
• At least one digit appears exactly twice
• No digit appears more than twice
• Order is irrelevant
• There are 38 such sequences
How do you test this?
Probably a bad idea:it "solves the problem" do expect(sequence.solutions.length).to eq(38)end
This is a little betterit "knows a valid sequence" do expect(Sequence.new(1, 1, 2, 3, 8)).to be_validend
And then we would make that test pass...
Wait,How would we make
that test pass?
class Sequence def init(*items) @items = items end
def valid? true endend
This is
OK
Because it is
notpermanent
Okay, now what
One option: break the testit "knows a valid sequence" do expect(Sequence.new(1, 1, 2, 3, 9)).not_to be_validend
Normally a good option, but
problematic here
Option 2, go smallerit "can tell if the sum is 15" do expect(Sequence.new(7, 8).to be_correct_sum)end
Why is that better?
Because it's small enough that I can write it without
thinking too hard
Also, I can negate it easilyit "can tell if the sum is not 15" do expect(Sequence.new(7, 8, 1).not_to be_correct_sum)end