Class WritablePropertyAssertion

Namespace: AutoFixture.Idioms
Assembly: AutoFixture.Idioms.dll

Encapsulates a unit test that verifies that a writable property is correctly implemented.

public class WritablePropertyAssertion : IdiomaticAssertion, IIdiomaticAssertion

Inheritance

objectIdiomaticAssertionWritablePropertyAssertion

Implements

IIdiomaticAssertion

Inherited Members

IdiomaticAssertion.Verify(params Assembly[]), IdiomaticAssertion.Verify(IEnumerable<Assembly>), IdiomaticAssertion.Verify(Assembly), IdiomaticAssertion.Verify(params Type[]), IdiomaticAssertion.Verify(IEnumerable<Type>), IdiomaticAssertion.Verify(Type), IdiomaticAssertion.Verify(params MemberInfo[]), IdiomaticAssertion.Verify(IEnumerable<MemberInfo>), IdiomaticAssertion.Verify(MemberInfo), IdiomaticAssertion.Verify(params ConstructorInfo[]), IdiomaticAssertion.Verify(IEnumerable<ConstructorInfo>), IdiomaticAssertion.Verify(FieldInfo), IdiomaticAssertion.Verify(params FieldInfo[]), IdiomaticAssertion.Verify(IEnumerable<FieldInfo>), IdiomaticAssertion.Verify(ConstructorInfo), IdiomaticAssertion.Verify(params MethodInfo[]), IdiomaticAssertion.Verify(IEnumerable<MethodInfo>), IdiomaticAssertion.Verify(MethodInfo), IdiomaticAssertion.Verify(params PropertyInfo[]), IdiomaticAssertion.Verify(IEnumerable<PropertyInfo>), IdiomaticAssertion.Verify(PropertyInfo), object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Remarks

According to the framework design guidelines, a writable property should always have both a setter and getter. In order not to violate the Principle of Least Astonishment the getter should always return the object assigned via the setter. Thus, for every writable property one should always write a unit test that verifies that the value read from the property is the same as the value assigned to the property.

However, such unit tests become tedious and repetitive to write; together, they form a unit testing idiom for .NET. This `IIdiomaticAssertion` encapsulates this idiom.

Constructors

WritablePropertyAssertion(ISpecimenBuilder)

Initializes a new instance of the WritablePropertyAssertion class.

public WritablePropertyAssertion(ISpecimenBuilder builder)

Parameters

builder ISpecimenBuilder

A composer which can create instances required to implement the idiomatic unit test, such as the owner of the property, as well as the value to be assigned and read from the property.

Remarks

builder will typically be a `Fixture` instance.

Properties

Builder

Gets the builder supplied via the constructor.

public ISpecimenBuilder Builder { get; }

Property Value

ISpecimenBuilder

Methods

Verify(PropertyInfo)

Verifies that a writable property is correctly implemented.

public override void Verify(PropertyInfo propertyInfo)

Parameters

propertyInfo PropertyInfo

The property.

Remarks

This method verifies that propertyInfo is correctly implemented as a writable property. It used the `Builder` to create an instance of the Type on which the property is implemented and then assigns to and reads from the property. The assertion passes if the value read from the property is the same as the value assigned to it. If this is not the case, a `WritablePropertyException` is thrown.

This method does nothing if the property is not a writable property.

Exceptions

WritablePropertyException

The verification fails.