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

I've done TDD wrong all the time (with livecode)

I've done TDD wrong all the time (with livecode)

Presentation done at ShareIt, with livecode

Repository available here: https://github.com/gixlg/bookstore

Luca Giuberti

November 23, 2023
Tweet

More Decks by Luca Giuberti

Other Decks in Programming

Transcript

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

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

    Unit test Mock Mock Mock Class reference
  3. © 2022 Thoughtworks Bookstore 7 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 9 1-2

    Contributor member Community member 4-5 Affiliate member 9-10 Premium member Deposit Withdraw 0 +1 +1 -2 -1 +1 +2 -1 (Discount) (Discount) (Bonus)
  5. © 2022 Thoughtworks 10 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 13 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? 14 We are defining a contract that the code will fulfill
  8. © 2022 Thoughtworks What do we do when we write

    a test? 15 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 18 Kent Beck
  10. © 2022 Thoughtworks TDD is not really about test but

    more about behavior and requirement 19 TDD // BDD // ATDD // Module test
  11. © 2022 Thoughtworks 21 21 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 22 Unit test Integration

    test UI test VS Fine Grained Unit test Coarse Grained Unit test Integration test UI test
  13. © 2022 Thoughtworks 23 23 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 24 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
  15. © 2022 Thoughtworks 26 Mock Mocks are useful for external

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

    pieces of the application that have complex logics or isolate subdomains of our application ⚠ trade-off
  17. © 2022 Thoughtworks I’ve found my balance 28 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 🤸
  18. © 2022 Thoughtworks References 29 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ć