Contract for verifying the implementation of a custom exception type.

Namespace:  MbUnit.Framework.ContractVerifiers
Assembly:  MbUnit (in MbUnit.dll) Version: 3.3.0.0 (3.3.610.0)

Syntax

C#
public class ExceptionContract<TException> : AbstractContract
where TException : Exception
Visual Basic (Declaration)
Public Class ExceptionContract(Of TException As Exception) _
	Inherits AbstractContract

Type Parameters

TException
The target custom exception type.

Remarks

Built-in verifications:

  • HasSerializableAttribute : The exception type has the SerializableAttribute attribute. Disable that test by settings the ImplementsSerialization property to false.
  • HasSerializationConstructor : The exception type has a protected serialization constructor similar to Exception(SerializationInfo, StreamingContext). Disable that test by settings the ImplementsSerialization property to false.
  • IsDefaultConstructorWellDefined : The exception type has a default parameter-less constructor. When the ImplementsSerialization property is set to true as well, the method verifies that the properties of the exception are preserved during a roundtrip serialization. Disable the test by settings the ImplementsStandardConstructors property to false.
  • IsMessageConstructorWellDefined : The exception type has single argument constructor for the message. When the ImplementsSerialization property is set to true as well, the method verifies that the properties of the exception are preserved during a roundtrip serialization. Disable the test by settings the ImplementsStandardConstructors property to false.
  • IsMessageAndInnerExceptionConstructorWellDefined : The exception type has two parameters constructor for the message and an inner exception. When the ImplementsSerialization property is set to true as well, the method verifies that the properties of the exception are preserved during a roundtrip serialization. Disable the test by settings the ImplementsStandardConstructors property to false.

Examples

The following example shows a simple custom exception class with some basic serialization support, and a test fixture which uses the exception contract to test it.
CopyC#
[Serializable]
public class SampleException : Exception, ISerializable
{
    public SampleException()
    {
    }

    public SampleException(string message)
        : base(message)
    {
    }

    public SampleException(string message, Exception innerException)
        : base(message, innerException)
    {
    }

    protected SampleException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }

    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
    }
}

public class SampleExceptionTest
{
    [VerifyContract]
    public readonly IContract ExceptionTests = new ExceptionContract<SampleException>()
    {
        ImplementsSerialization = true, // Optional (default is true)
        ImplementsStandardConstructors = true // Optional (default is true)
    };
}

Inheritance Hierarchy

System..::.Object
  MbUnit.Framework.ContractVerifiers..::.AbstractContract
    MbUnit.Framework.ContractVerifiers..::.ExceptionContract<(Of <(TException>)>)

See Also