It should! - BDD in practice

  • 737 views
Uploaded on

How BDD helps you solve the murder on the Orient Express

How BDD helps you solve the murder on the Orient Express

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
737
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
18
Comments
1
Likes
7

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. IT SHOULD! BDD IN PRACTICE
  • 2. BEHAVIOR DRIVEN DEVELOPMENT = TEST DRIVEN DEVELOPMENT exceptit's done right
  • 3. TDD IS CODE-CENTRIC WHILE BDD IS COMMUNICATION-CENTRIC
  • 4. IT'S LIKE COKE PEPSI = COCA-COLA exceptit's done right
  • 5. BUT THIS IS NOT ABOUT WHAT BDD MEANS THIS IS ALL ABOUT TOOLS AND ...
  • 6. THE MURDER ON THE ORIENT EXPRESS
  • 7. MA'AM! CAN YOU TEST WHO YOU SAW? "Whatdo you mean test?Ican describe itif you like"-Ma'am ./bin/phpspecdescSuspect
  • 8. THE SUSPECTS "He was one of those types, you know"-Ma'am namespacespecOrientExpressSuspect; usePhpSpecObjectBehavior; useProphecyArgument; classSuspectSpecextendsObjectBehavior { functionit_is_initializable() { $this->shouldHaveType('OrientExpressSuspectSuspect'); } }
  • 9. THE SUSPECTS "Don'tyell atme!Be adear and help me out."-Ma'am
  • 10. THE SUSPECTS namespacespecOrientExpressSuspect; usePhpSpecObjectBehavior; useProphecyArgument; classSuspectSpecextendsObjectBehavior { //... functionit_should_answer_questions() { $this->answer('Areyouthekiller?') ->shouldReturn('Nope,notme'); } }
  • 11. THE SUSPECTS
  • 12. THE SUSPECTS namespaceOrientExpressSuspect; classSuspect { publicfunctionanswer($question) { return'Nope,notme'; } }
  • 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. 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. THE SUSPECTS namespaceOrientExpressSuspect; classSuspect { protected$keyQuestion='Itwasyou!'; protected$guilty=false; publicfunctionanswer($question) { if($this->guilty&&$question==$this->keyQuestion){ return'MmmkayIconfess'; } return'Nope,notme'; } //... }
  • 16. SIR! CAN YOU DESCRIBE WHO SOLVED IT? ./bin/phpspecdescDetective
  • 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. 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. 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. WORKS AS DESCRIBED
  • 21. WHAT ABOUT THE SCOTLAND YARD?
  • 22. DOES THE POLICE FUNCTION PROPERLY? Feature: Inordertoknowthecrimesonorientexpress AsaScotlandYardAPIclient Iwanttogetaccesstothearchives
  • 23. DOES THE POLICE FUNCTION PROPERLY? Background: Giventhefollowingcrimesexist: |name | |"MurderontheOrientExpress"| Andthefollowingdetectivesexist: |name | |"HerculsePoirot"| Andthefollowingsuspectsexist: |name | |"PrincessDragomiroff"| |"CountAdrenyi" |
  • 24. DOES THE POLICE FUNCTION PROPERLY?
  • 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. WHY IS THIS SO IMPORTANT? BECAUSE EVERY CODE TELLS A STORY OR AT LEAST IT SHOULD!
  • 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