|
| 1 | +include: package:solid_lints/analysis_options.yaml |
| 2 | + |
| 3 | +dart_code_metrics: |
| 4 | + rules: |
| 5 | + |
| 6 | +# Late keyword is allowed in tests in order to enable the use of custom mocks and |
| 7 | +# fakes. |
| 8 | + |
| 9 | +# It has several benefits over its alternatives (see below): |
| 10 | + |
| 11 | +# - Allows initialising the mocks inside the `setUp` method. |
| 12 | +# - Allows resetting mocks by re-initialising them inside `setUp` |
| 13 | +# - Allows passing the mock inside functions that require non-null params, |
| 14 | +# without any extra force-unwraps or null checks. |
| 15 | +# - Uninitialized test mocks are clearly traceable. |
| 16 | +# - Prodices minimal visual clutter. |
| 17 | + |
| 18 | +# Alternatives considered: |
| 19 | + |
| 20 | +# 1. Making the mocks `final` and non-nullable, and adding a `reset()` method |
| 21 | +# to them, which would return the mock/fake to its initial state. |
| 22 | +# - Works well with generated code from testing libraries like `mockito`. |
| 23 | +# - It's less practical with hand-written mocks, where it's possible to add a |
| 24 | +# piece of state and forget to reset it, which might lead to hard-to-trace |
| 25 | +# errors. Usually re-instantiating a test mock simplifies its code and |
| 26 | +# prevents such errors altogether, but that requires making the field |
| 27 | +# non-final, as well as delegating its initialisation to the `setUp` method. |
| 28 | +# 2. Making the mocks nullable, and using `.?` access syntax. |
| 29 | +# - In many cases will cause harder-to-trace testcase failures, if a mock was |
| 30 | +# not initialised. |
| 31 | +# - Does not allow passing the nullable mock into constructors/methods that |
| 32 | +# require a non-nullable input. |
| 33 | +# 3. Making the mocks nullable, and adding null-checks in test code. |
| 34 | +# - Will lead to significant code bloat, without much benefit, as a null mock |
| 35 | +# will still usually lead to a failed test. |
| 36 | +# 4. Making the mocks nullable and using the "bang" operator (`!`): |
| 37 | +# - In terms of behavior similar to `late`, but requires using the operator in |
| 38 | +# many places inside the test code, adding uninformative visual noise. |
| 39 | +# - The use of this operator is also discouraged by the main ruleset. |
| 40 | + avoid-late-keyword: false |
0 commit comments