Eliminate conditional logic in \Drupal\Tests\package_manager\Kernel\FileSyncerFactoryTest::testFactory()

Created on 11 September 2023, over 1 year ago

tldr: This MR provides an example of clarifying/simplifying tests by removing conditional logic from them.

I've opined before about the importance of banishing conditional logic from unit tests. I like this statement of the best practice:

Unless you want to write unit tests for your unit tests, don’t add calculations or logic to your unit tests. The code needs to be simple, with a cyclomatic complexity of one.

Logic obscures the purpose of the tests, and even with the purpose explained, the complexity of the code may reduce the readers confidence in the test.

The below MR provides an example of doing this by converting conditional logic in \Drupal\Tests\package_manager\Kernel\FileSyncerFactoryTest::testFactory() with a simple values in the data provider. Observe how it 1) makes the test method itself terse, readable, and clear. It would be difficult to misunderstand. 2) The data provider contains both the condition and the expectation, making it a set of complete, declarative statements. The test case is separated into its two logical parts: the expectations, and the behavior under tests. Further benefits might be enumerated, but I leave them to the imagination of the reader. πŸ˜‰

* "There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies." - Tony Hoare

πŸ“Œ Task
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States traviscarden

Live updates comments and jobs are added and updated live.
  • Quick fix

    Very small and simple change. Preferred over Quickfix.

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024