It should! - BDD in practice

1,214 views
1,149 views

Published on

How BDD helps you solve the murder on the Orient Express

1 Comment
9 Likes
Statistics
Notes
No Downloads
Views
Total views
1,214
On SlideShare
0
From Embeds
0
Number of Embeds
26
Actions
Shares
0
Downloads
33
Comments
1
Likes
9
Embeds 0
No embeds

No notes for slide

It should! - BDD in practice

  1. 1. IT SHOULD! BDD IN PRACTICE
  2. 2. BEHAVIOR DRIVEN DEVELOPMENT = TEST DRIVEN DEVELOPMENT exceptit's done right
  3. 3. TDD IS CODE-CENTRIC WHILE BDD IS COMMUNICATION-CENTRIC
  4. 4. IT'S LIKE COKE PEPSI = COCA-COLA exceptit's done right
  5. 5. BUT THIS IS NOT ABOUT WHAT BDD MEANS THIS IS ALL ABOUT TOOLS AND ...
  6. 6. THE MURDER ON THE ORIENT EXPRESS
  7. 7. MA'AM! CAN YOU TEST WHO YOU SAW? "Whatdo you mean test?Ican describe itif you like"-Ma'am ./bin/phpspecdescSuspect
  8. 8. THE SUSPECTS "He was one of those types, you know"-Ma'am namespacespecOrientExpressSuspect; usePhpSpecObjectBehavior; useProphecyArgument; classSuspectSpecextendsObjectBehavior { functionit_is_initializable() { $this->shouldHaveType('OrientExpressSuspectSuspect'); } }
  9. 9. THE SUSPECTS "Don'tyell atme!Be adear and help me out."-Ma'am
  10. 10. THE SUSPECTS namespacespecOrientExpressSuspect; usePhpSpecObjectBehavior; useProphecyArgument; classSuspectSpecextendsObjectBehavior { //... functionit_should_answer_questions() { $this->answer('Areyouthekiller?') ->shouldReturn('Nope,notme'); } }
  11. 11. THE SUSPECTS
  12. 12. THE SUSPECTS namespaceOrientExpressSuspect; classSuspect { publicfunctionanswer($question) { return'Nope,notme'; } }
  13. 13. THE SUSPECTS - REFACTORING MADE EASY "Oh, Ijustremembered!"-Ma'am namespacespecOrientExpressSuspect; usePhpSpecObjectBehavior; useProphecyArgument; classSuspectSpecextendsObjectBehavior { //... functionit_should_not_confess_to_wrong_questions_if_guilty() { $this->setGuilty(true); $this->answer('Areyouthekiller?') ->shouldReturn('Nope,notme'); } }
  14. 14. THE SUSPECTS - REFACTORING MADE EASY "Oh, Ijustremembered!"-Ma'am namespacespecOrientExpressSuspect; usePhpSpecObjectBehavior; useProphecyArgument; classSuspectSpecextendsObjectBehavior { //... functionit_should_confess_to_key_question_if_guilty() { $this->setGuilty(true); $this->answer('Itwasyou!') ->shouldReturn('MmmkayIconfess'); } }
  15. 15. THE SUSPECTS namespaceOrientExpressSuspect; classSuspect { protected$keyQuestion='Itwasyou!'; protected$guilty=false; publicfunctionanswer($question) { if($this->guilty&&$question==$this->keyQuestion){ return'MmmkayIconfess'; } return'Nope,notme'; } //... }
  16. 16. SIR! CAN YOU DESCRIBE WHO SOLVED IT? ./bin/phpspecdescDetective
  17. 17. THE OLDSCHOOL DETECTIVE //OppaPHPUnitstyle functiontestSolve() { //... $crime=$this->getMock('OrientExpressCrimeCrime', array('getSuspects')); $crime->expects($this->any()) ->method('getSuspects') ->will($this->returnValue(array($suspect1,$suspect2)); //NoOoOoooOoOoOoo!!! }
  18. 18. THE DETECTIVE classDetectiveSpecextendsObjectBehavior { //... /** *@paramOrientExpressCrimeCrime$crime *@paramOrientExpressSuspectSuspect$suspect1 *@paramOrientExpressSuspectSuspect$suspect2 */ functionit_should_be_able_to_solve_crimes($crime,$suspect1, $suspect2) { $crime->getSuspects() ->willReturn(array($suspect1,$suspect2)); $suspect1->answer('Itwasyou!') ->willReturn('Nope,notme'); $suspect2->answer('Itwasyou!') ->willReturn('MmmkayIconfess'); $this->solve($crime)->shouldReturn($suspect2); } }
  19. 19. THE DETECTIVE namespaceOrientExpressInvestigators; useOrientExpressCrimeCrime; classDetective { publicfunctionsolve(Crime$crime) { foreach($crime->getSuspects()as$suspect){ $answer=$suspect->answer('Itwasyou!'); if(preg_match('/confess/',$answer)){ return$suspect; } } } }
  20. 20. WORKS AS DESCRIBED
  21. 21. WHAT ABOUT THE SCOTLAND YARD?
  22. 22. DOES THE POLICE FUNCTION PROPERLY? Feature: Inordertoknowthecrimesonorientexpress AsaScotlandYardAPIclient Iwanttogetaccesstothearchives
  23. 23. DOES THE POLICE FUNCTION PROPERLY? Background: Giventhefollowingcrimesexist: |name | |"MurderontheOrientExpress"| Andthefollowingdetectivesexist: |name | |"HerculsePoirot"| Andthefollowingsuspectsexist: |name | |"PrincessDragomiroff"| |"CountAdrenyi" |
  24. 24. DOES THE POLICE FUNCTION PROPERLY?
  25. 25. Scenario:GettingdatafromtheScotlandYard'sarchives Givencrime"MurderontheOrientExpress"wassolvedby"HerculesPoirot " Andcrime"MurderontheOrientExpress"hadsuspects: |name | |"PrincessDragomiroff"| |"CountAdrenyi" | WhenIsendGETrequestto"/api/crimes/ORIENT_EXPRESS_CRIME_ID" Thentheresponseshouldcontainjson: """ { "crime":{ "name":"MurderontheOrientExpress", "detective":{ "name":"HerculesPoirot" }, "suspects":[ { "name":"PrincesDragomiroff" }, { "name":"CountAdrenyi" } ] } } """
  26. 26. WHY IS THIS SO IMPORTANT? BECAUSE EVERY CODE TELLS A STORY OR AT LEAST IT SHOULD!
  27. 27. ? PhpSpec: Behat: Specs: ABOUT ME KarolSójko LeadSoftware Architect @ Twitter: Github: https://github.com/phpspec/phpspec https://github.com/Behat/Behat https://github.com/karolsojko/bdd-in-practice The CometCult @karolsojko karolsojko

×