ExpectedExceptionAttribute Constructor (Type, String)
Visual Studio 2012
Initializes a new instance of the ExpectedExceptionAttribute class with and expected exception type and a message that describes the exception.
Assembly: Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)
The following class contains the method to test:
using System; namespace MyCSNamespace { public class DivisionClass { public int Divide(int numerator, int denominator) { return numerator / denominator; } } }
The following test method tests the Divide method of the DivisionClass object. It tests for the existence of a DivideByZeroException.
using Microsoft.VisualStudio.TestTools.UnitTesting; using MyCSNamespace; namespace MyCSTestProject { [TestClass()] public class DivisionClassTest { [TestMethod()] [ExpectedException(typeof(System.DivideByZeroException), "MyMessage")] public void DivideTest() { DivisionClass target = new DivisionClass(); int numerator = 4; int denominator = 0; int actual; actual = target.Divide(numerator, denominator); } } }
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.