Contract for verifying the implementation of public type accessors, usually the getter and the setter of a particular property.

Namespace:  MbUnit.Framework.ContractVerifiers
Assembly:  MbUnit (in MbUnit.dll) Version: 3.2.0.0 (3.2.528.0)

Syntax

C#
public class AccessorContract<TTarget, TValue> : AbstractContract
Visual Basic (Declaration)
Public Class AccessorContract(Of TTarget, TValue) _
	Inherits AbstractContract

Type Parameters

TTarget
The type of the tested object which contain the accessors.
TValue
The type of the value handled by the accessors.

Remarks

Built-in verifications:

  • SetValidValues : The setter accepts the values specified in the ValidValues contract property as valid assignment values, and the getter returns equal values (object equality) when called afterwards.
  • SetInvalidValues : The setter rejects the values specified in the InvalidValues contract property by throwing the appropriate exception. The test is not run when the InvalidValues contract property is left empty.
  • SetNullValue : The setter rejects or accepts a null reference value according to the state of the AcceptNullValue contract property. If set to true, the setter is expected to accept a null reference as a valid value, and the getter to return a null reference as well. If set to false, the setter is expected to reject a null reference assignment by throwing a ArgumentNullException exception. If the type handled by the tested accessors is not nullable (a value type by example), the test is not run.

Examples

The following examples shows a simple class that contains a property, and a test fixture which declares an accessor contract to verify the expected behavior of the subject property.
CopyC#
public class Foo
{
    private string name;

    public string Name
    {
        get
        {
            return name;
        }

        set
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (value.Length == 0)
            {
                throw new ArgumentException("Cannot be an empty string.", "value");
            }

            name = value;
        }
    }
}

[TestFixture]
public class FooTest
{
    [VerifyContract]
    public readonly IContract NameAccessorTests = new AccessorContract<Foo, string>
    {
        PropertyName = "Name",
        AcceptNullValue = false,
        ValidValues = { "Value1", "Value2" },
        InvalidValues =
        {
            { typeof(ArgumentException), String.Empty }
        }
    };
}

Inheritance Hierarchy

System..::.Object
  MbUnit.Framework.ContractVerifiers..::.AbstractContract
    MbUnit.Framework.ContractVerifiers..::.AccessorContract<(Of <(TTarget, TValue>)>)

See Also