Too many mocks killed the test: What Hexagonal Architecture has changed

Too many mocks killed the test: What Hexagonal Architecture has changed

64822af642dacc617380f009e35a78f7?s=128

Jean-Marie Lamodière

October 22, 2020
Tweet

Transcript

  1. Icons made by Icongeek26, Freepik from www.flaticon.com

  2. Locking Tests vs. TDD Icons made by Roundicons from www.flaticon.com

  3. Icons made by Freepik from www.flaticon.com https://github.com/JMLamodiere/tdd-demo-forumphp2020

  4. Previously...

  5. • A Functional Test describes a functionality • A Unit

    Test locks an implementation Prejudice #1 Icons made by Freepik from www.flaticon.com
  6. • A Functional Test describes a behaviour • A Unit

    Test locks an implementation Prejudice #1 Icons made by Roundicons from www.flaticon.com All tests describe the expected public behaviour Only the audience and the size of the covered area vary
  7. Only functional tests have 3 steps: • Given • When

    • Then Prejudice #2 Icons made by Freepik from www.flaticon.com
  8. Only functional tests have 3 steps: • Given • When

    • Then Prejudice #2 Icons made by Roundicons from www.flaticon.com All tests follow the narrative structure: • Given = Arrange • When = Act • Then = Assert Setup Catalyst Resolution
  9. Prejudice #3 Icons made by Freepik from www.flaticon.com Checking the

    number of calls to all mocked methods increases my confidence. ->expects($this->exactly(1))->method('xxx') ->shouldBeCalledTimes(1)
  10. Prejudice #3 Checking the number of calls to all mocked

    methods increases my confidence ->expects($this->exactly(1))->method('xxx') ->shouldBeCalledTimes(1) Icons made by Roundicons, Smashicons from www.flaticon.com • Blurs the intent of the test • Locks implementation details • Impossible to write first
  11. What to test ?

  12. Prejudice #4 Icons made by Freepik from www.flaticon.com I can

    mock the libraries used on the Infrastructure side : • Http : Guzzle… • Database : Doctrine, Eloquent…
  13. Prejudice #4 Icons made by Roundicons, Smashicons from www.flaticon.com I

    can mock the libraries used on the Infrastructure side : • Http : Guzzle… • Database : Doctrine, Eloquent… • Coupled with the usage details of the lib • Assumptions about the way it behaves • Impossible to write first
  14. Infrastructure : Integration Tests Icons made by Freepik, Smashicons, Roundicons

    from www.flaticon.com
  15. Prejudice #5 Icons made by Freepik from www.flaticon.com Functional Test

    = necessarily end to end
  16. Prejudice #5 Icons made by Roundicons, smashicons from www.flaticon.com Functional

    Test = necessarily end to end • Slow • Brittle • Difficult to maintain • Harder to write first • Locks Infrastructure choices too soon Suggestion : mock Secondary Ports
  17. Icons made by Freepik from www.flaticon.com Suggestion

  18. I must know the splitting between my classes before I

    write my tests Prejudice #6 Icons made by Freepik from www.flaticon.com
  19. I must know the splitting between my classes before I

    write my tests Prejudice #6 Icons made by Roundicons from www.flaticon.com • Prod classes splitting != test classes splitting • Start with an area with a known boundary
  20. Icons made by Freepik from www.flaticon.com 1st test: area with

    a known boundary
  21. Prejudice #7 Icons made by Freepik from www.flaticon.com If I

    don't mock my Entities / DTOs, too many tests will break if I add a field
  22. Prejudice #7 Icons made by Roundicons from www.flaticon.com If I

    don't mock my Entities / DTOs, too many tests will break if I add a field • Use test Factori(es) with optional fields • Getter not covered ? I remove it
  23. Thanks! Icons made by Roundicons, Freepik from www.flaticon.com It’s your

    turn ! • •