Freigeben über


ExpectedExceptionAttribute-Konstruktor (Type, String)

Initialisiert eine neue Instanz der ExpectedExceptionAttribute-Klasse mit einem erwarteten Ausnahmetyp und einer Meldung, die die Ausnahme beschreibt.

Namespace:  Microsoft.VisualStudio.TestTools.UnitTesting
Assembly:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)

Syntax

'Declaration
Public Sub New ( _
    exceptionType As Type, _
    noExceptionMessage As String _
)
public ExpectedExceptionAttribute(
    Type exceptionType,
    string noExceptionMessage
)
public:
ExpectedExceptionAttribute(
    Type^ exceptionType, 
    String^ noExceptionMessage
)
new : 
        exceptionType:Type * 
        noExceptionMessage:string -> ExpectedExceptionAttribute
public function ExpectedExceptionAttribute(
    exceptionType : Type, 
    noExceptionMessage : String
)

Parameter

  • exceptionType
    Typ: Type

    Ein erwarteter Typ von Ausnahme, die von einer Methode ausgelöst wird.

  • noExceptionMessage
    Typ: String

Hinweise

Wenn exceptionType oder message den Wert nullein Nullverweis (Nothing in Visual Basic) hat, wird eine Diagnosemeldung an einen Ablaufverfolgungslistener gesendet.

Beispiele

Die folgende Klasse enthält die zu testende Methode:

using System;

namespace MyCSNamespace
{
    public class DivisionClass
    {
        public int Divide(int numerator, int denominator)
        {
            return numerator / denominator;
        }
    }
}
Public Class DivisionClass
   Public Function Divide(ByVal numerator As Integer, ByVal denominator As Integer) As Integer
      Return numerator \ denominator
   End Function
End Class

Die folgende Testmethode testet die Divide-Methode des DivisionClass-Objekts. Es wird auf das Vorkommen von DivideByZeroException getestet.

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);
        }
    }
}
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports MyVBProject

<TestClass()> _
Public Class DivisionClassTest
    <TestMethod()> _
    <ExpectedException(GetType(System.DivideByZeroException), "MyMessage")> _
    Public Sub DivideTest()
        Dim target As DivisionClass = New DivisionClass
        Dim numerator As Integer = 4
        Dim denominator As Integer = 0
        Dim actual As Integer
        actual = target.Divide(numerator, denominator)
    End Sub
End Class

.NET Framework-Sicherheit

Siehe auch

Referenz

ExpectedExceptionAttribute Klasse

ExpectedExceptionAttribute-Überladung

Microsoft.VisualStudio.TestTools.UnitTesting-Namespace