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

Too many mocks killed the test: What Hexagonal ...

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

Jean-Marie Lamodière

October 22, 2020
Tweet

More Decks by Jean-Marie Lamodière

Other Decks in Programming

Transcript

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

    Test locks an implementation Prejudice #1 Icons made by Freepik from www.flaticon.com
  2. • 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
  3. Only functional tests have 3 steps: • Given • When

    • Then Prejudice #2 Icons made by Freepik from www.flaticon.com
  4. 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
  5. 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)
  6. 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
  7. 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…
  8. 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
  9. 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
  10. I must know the splitting between my classes before I

    write my tests Prejudice #6 Icons made by Freepik from www.flaticon.com
  11. 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
  12. 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
  13. 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