-
-
Notifications
You must be signed in to change notification settings - Fork 540
Design Guidelines
Dennis Doomen edited this page Apr 20, 2024
·
1 revision
- High-level principles such as written down in https://github.com/fluentassertions/fluentassertions/issues/1281#issuecomment-636414698
- Behavioral patterns such as how to deal with
null
arguments - When to return
AndWhichConstraint
vsAndConstraint
. - When to use
FailWith
vs throwing a normal .NET exception type - Prefer "Did not expect something to be" over "Expected something not to be"
- Decisions (like a poor mans Architecture Design Log) such as why we target certain frameworks
- When using
AndWhichConstraint<..., T>
theT
element must be fetched in way resilient to thatFailWith
don't throw immediately when encapsulated in a customAssertionScope
. - Assertion class over non-sealed classes or interfaces should probably be generic in type to avoid loosing type information.
E.g instead of
MyClassAssertions
thenMyClassAssertions<T> where T : MyClass
- Be aware that an assertion might be wrapped in an
AssertionScope
soFailWith
does not halts execution and extra precautions must be taken to avoid e.g. enNullReferenceException
after verifying thatSubject is not null
- Don't use
predicate.Body
to format a predicate in a failure message. Just pass the predicate toFailWith
and let thePredicateLambdaExpressionValueFormatter
handle formatting it properly - Naming and grouping guidelines (based on this post)
- Group tests for the same API using a nested class such as in NullableNumericSpecs so you don't have to repeat the API name in the name of the test
- Avoid the use of
When
andShould
in test names and use concise names likeExclusion_of_missing_members_works_with_mapping
- When writing parameterized tests prefer using
[InlineData]
, unless:- the values are not compile-time constants, or
- there is a good reason to share the parameters between multiple tests.
- Don't use
Should().NotThrow
in the assert.
- String formatting should use Invariant culture when formatting non-strings.
- Lines should not be wider than 130 characters.
- Prefer
is null
andis not null
over!=/== null