AutoFixture.Idioms
help creating unit tests that quickly check if these best practices are properly followed.AutoFixture.Idioms
package contains several assertions. Each assertion encapsulates a unit test that tests a specific expectation on the system under test.IIdiomaticAssertion
. This interface exposes a plethora of overloads of the Verify
methods accepting everything from a set of assemblies down to a single member.IdiomaticAssertion
offers a basic implementation of all overloads but the ones at the end of the tree (constructors, methods, properties, fields).IdiomaticAssertion
class works, developers can target a specific member or the whole type. It will be up to the author of the assertion to make sure that only the suitable members are tested.AutoFixture.Idioms
package.GuardClauseAssertion
verifies that the parameters passed to a method or a constructor are properly checked against null values.string
as dependencies):N+1
unit tests, only for the constructor: one for each incoming parameter and one for the correct initialization of the system under test.ConstructorInitializedMemberAssertion
can be used to verify that these properties have been initialized with the value passed to the constructor via same-name arguments.ConstructorInitializedMemberAssertion
can be used to verify that the properties Parameter
and Value
contain the same values passed to the constructor.TestClass
constructor will cause the test in the snippet above to fail.Object.Equals
and Object.GetHashCode
.a == a
(a == b) => (b == a)
(a == b, b == c) => (a == c)
null
,Object.Equals
must override Object.GetHashCode
too,Object.Equals
to the same object must return the same result,Object.GetHashCode
to the same object must return the same result,new object()
must return false
.AutoFixture.Idioms
contains assertions that can reduce the amount of code needed for the tests.EqualsNewObjectAssertion
verifies that Object.Equals
is implemented so that x.Equals(new object())
is always false
,EqualsNullAssertion
verifies that Object.Equals
is implemented so that x.Equals(null))
is always false
,EqualsSelfAssertion
verifies that Object.Equals
is implemented so that x.Equals(x))
is always true
,EqualsSuccessiveAssertion
verifies that Object.Equals
is implemented so that calling x.Equals(y)
several times returns always the same value,GetHashCodeSuccessiveAssertion
verifies that Object.GetHashCode
is implemented so that calling x.GetHashCode()
several times returns always the same value.CompositeIdiomaticAssertion
abstract class.EqualityAssertion
as followsEqualityAssertion
class by addition additional child assertions.IEqualityComparer<T>
.bool Equals(T,T)
and int GetHashCode(T)
, and the same rules applying for value equality need to be followed when implementing this interface.EqualityComparerAssertion
that can be used to validate the implementation of the custom equality comparer.SampleValueObject
displayed aboveEqualityComparerAssertion
, we can test all the equality properties listed above in a single unit test.