Slide 1

Slide 1 text

ADVANCED TDD Diego Aguiar Beltrán @MolloKhan

Slide 2

Slide 2 text

➢ Developer at SymfonyCasts ➢ Hiking ➢ Gaming (Warcraft FTW!) ➢ Tepic, Mexico Diego Aguiar Beltrán @MolloKhan

Slide 3

Slide 3 text

Main Topics ➢ What TDD Really is ➢ Useful TDD Techniques ➢ What “Getting Stuck” means ➢ When Not to TDD

Slide 4

Slide 4 text

What is TDD? ➢ Programming Methodology ➢ It’s Composed of 3 Laws ➢ Tests Always Go First ➢ Red, Green, Refactor cycle

Slide 5

Slide 5 text

TDD is more like a discipline

Slide 6

Slide 6 text

➢ Practice, practice, practice… ➢ Train Your Mind ➢ Tests Go First

Slide 7

Slide 7 text

A bit of History… ➢ Kent Beck inventor of TDD ➢ Invented In the late 1990’s ➢ As a way to write better quality code

Slide 8

Slide 8 text

TDD Techniques ➢ Fake it till you make it ➢ Triangulation ➢ Assert First

Slide 9

Slide 9 text

Fake it till you make it

Slide 10

Slide 10 text

Name Inverter Example “Diego Aguiar” → “Aguiar, Diego” Fake it till you make it

Slide 11

Slide 11 text

Where to start? Fake it till you make it

Slide 12

Slide 12 text

Fake it till you make it public function testNullCase() { $name = null; $invertedName = $this->nameInverter ->invert($name); self::assertEquals('', $invertedName); } public function invert($name) { // Forcing test to pass return ‘’; } TEST CODE PRODUCTION

Slide 13

Slide 13 text

public function invert($name) { return 'Diego'; } PRODUCTION Fake it till you make it public function testSingleName() { $name = 'Diego'; $invertedName = $this->nameInverter ->invert($name); self::assertEquals( 'Diego', $invertedName ); } TEST CODE

Slide 14

Slide 14 text

Fake it till you make it PRODUCTION public function invert($name) { if (!$name) { return ''; } return $name; }

Slide 15

Slide 15 text

Triangulation

Slide 16

Slide 16 text

Triangulation class NameInverterTest extends TestCase { public function testNullCase() { $invertedName = $this->nameInverter->invert(null); self::assertEquals('', $invertedName); } public function testSingleName() { $invertedName = $this->nameInverter->invert('Diego'); self::assertEquals('Diego', $invertedName); } }

Slide 17

Slide 17 text

Fake it till you make it & Triangulation The Best of Friends

Slide 18

Slide 18 text

Assert First

Slide 19

Slide 19 text

Assert First class NameInverterTest extends TestCase { public function testFullName() { $name = 'Diego Aguiar'; $invertedName = $this->nameInverter ->invert($name); self::assertEquals('Aguiar, Diego', $invertedName); } }

Slide 20

Slide 20 text

Getting Stuck

Slide 21

Slide 21 text

Why does it happen? ➢ It’s a symptom of a problem ➢ Production code is too specific ➢ You wrote the wrong test ➢ Aiming for the goal too fast

Slide 22

Slide 22 text

How to get Unstuck ➢ Step up and think for a simpler test ➢ Refactor hard-to-test code ➢ Write a list of use-cases ➢ Ignore testing for a moment

Slide 23

Slide 23 text

When Not to TDD ➢ Configuration ➢ Discovering ➢ Repository Queries

Slide 24

Slide 24 text

➢ TDD is more like a discipline ➢ There are techniques to get you unstuck ➢ TDD is a tool, not a rule Conclusion @MolloKhan

Slide 25

Slide 25 text

Happy TDD! @MolloKhan “One test a day keeps bugs away"

Slide 26

Slide 26 text

Z zero (or empty case) O one M many B boundaries I interfaces E exceptions BONUS!! https://i.ytimg.com/vi/0xRCYmbvfEs/maxresdefault.jpg

Slide 27

Slide 27 text

Happy TDD! @MolloKhan THANK YOU AGAIN!