ExpectedExceptionAttribute 构造函数 (Type, String)

用预期的异常类型以及描述此异常的消息来初始化 ExpectedExceptionAttribute 类的新实例。

命名空间:  Microsoft.VisualStudio.TestTools.UnitTesting
程序集:  Microsoft.VisualStudio.QualityTools.UnitTestFramework(在 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 中)

语法

声明
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
)

参数

  • exceptionType
    类型:System.Type
    预期的异常类型,此异常将由方法引发。

备注

如果 exceptionType 或 message 为 nullnull 引用(在 Visual Basic 中为 Nothing), 诊断消息将被发送到跟踪侦听器。

示例

下面的类包含要测试的方法:

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

下面的测试方法将测试 DivisionClass 对象的 Divide 方法。 它测试是否存在 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);
        }
    }
}
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 安全性

请参见

参考

ExpectedExceptionAttribute 类

ExpectedExceptionAttribute 重载

Microsoft.VisualStudio.TestTools.UnitTesting 命名空间