Upgrade to Pro — share decks privately, control downloads, hide ads and more …

I've done TDD wrong all the time

I've done TDD wrong all the time

Avatar for Luca Giuberti

Luca Giuberti

October 16, 2022
Tweet

More Decks by Luca Giuberti

Other Decks in Programming

Transcript

  1. © 2022 Thoughtworks The benefit that was promised 3 Remove

    the fear of refactoring VS Unit test Mock
  2. © 2022 Thoughtworks What are Unit tests and Mocks 4

    Unit test Mock Mock Mock Class reference
  3. © 2022 Thoughtworks Bookstore 6 Has been asked to implement

    a ethical bookstore that that doesn't accept money It has two operation: • Withdraw a book • Deposit a book Withdraw a book it cost 2 credit points. If we have at least 5 credit we get a discount of 1 credit point. Deposit a book gives 1 credit point. If we have 10 or more credit points he will receive 1 credit point as extra bonus.
  4. © 2022 Thoughtworks Book store - State pattern 14 1-2

    Contributor member Community member 4-5 Affiliate member 9-10 Premium member Deposit Withdraw 0 +1 +1 -2 -1 +1 +2 -1
  5. © 2022 Thoughtworks 22 What we have now • The

    tests don’t speak about “What” the application does but they are speaking about “How” • Tests are far to be a design tool • Fragile tests • For a few moment we gave up our safety-net
  6. © 2022 Thoughtworks What happened 26 Tests/mocks became an obstacle

    to the refactoring Write a failing test Make the test pass Refactor TDD We changed our code for some implementation details
  7. © 2022 Thoughtworks What do we do when we write

    a test? 27 We are defining a contract that the code will fulfill
  8. © 2022 Thoughtworks What do we do when we write

    a test? 28 We are defining a contract that the code will fulfill If it’s tight to implementation details it will easily breaks
  9. © 2022 Thoughtworks Tests should be coupled to the behavior

    of code and decoupled from the structure of code 31 Kent Beck
  10. © 2022 Thoughtworks TDD is not really about test but

    more about behavior and requirement 32 TDD // BDD // ATDD // Module test
  11. © 2022 Thoughtworks 33 33 Lots of confusion has been

    made around the name Unit test Coarse grained unit test Tests are coupled to the behaviours Fine grained unit test Tests are coupled to the implementation
  12. © 2022 Thoughtworks The Test Diamond 34 Unit test Integration

    test UI test VS Fine Grained Unit test Coarse Grained Unit test Integration test UI test
  13. © 2022 Thoughtworks 35 35 Combinatory logics Write tests for

    the logic condition All possible cases from the implementation point of view: • 3 + 1 = 4 (n+1) Write all possible scenario All possible cases from a requirement point of view are: • 2^3 = 9 (2^n) Give a free book if: A and (B or C) i.e. A = Deposit operation B = Is user’s birthday C = User has visited the shop 3 times in a week ⚠ trade-off
  14. © 2022 Thoughtworks 36 TDD - Refactoring 🟢 If the

    requirement doesn’t change The test shouldn't change either
  15. © 2022 Thoughtworks 37 What should we do when we

    write a test? Put yourself in the user’s shoes, tests should be executable specification Develop against an interface Outside-in
  16. © 2022 Thoughtworks 39 Mock Mocks are useful for external

    dependency We should mock the infrastructure layer (through an adapter) i.e. DB, Kafka, HTTP call
  17. © 2022 Thoughtworks 40 Mock Mocks are useful for isolate

    pieces of the application that have complex logics or isolate subdomains of our application ⚠ trade-off
  18. © 2022 Thoughtworks I’ve found my balance 41 That’s how

    I’ve removed the fear of refactoring • Unit tests based on behaviours • Test against an interface • Do not mock logic, mock the infrastructure Your balance might be different 🤸
  19. © 2022 Thoughtworks References 42 Books: Videos: TDD, Where Did

    It All Go Wrong Ian Cooper Outside-in diamond ◆ TDD Thomas Pierrain Automated tests: You won't find it in a book! Jacek Milewski TDD and Clean Architecture - Driven by Behaviour Valentina Cupać