共用方式為


ExpectedExceptionBaseAttribute 類別

這是屬性的基底類別,這些屬性指定單元測試應擲回預期的例外狀況。

繼承階層架構

Object
  Attribute
    Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionBaseAttribute
      Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionAttribute

命名空間:  Microsoft.VisualStudio.TestTools.UnitTesting
組件:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (在 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 中)

語法

'宣告
<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple := False, Inherited := True)> _
Public MustInherit Class ExpectedExceptionBaseAttribute _
    Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public abstract class ExpectedExceptionBaseAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Method, AllowMultiple = false, Inherited = true)]
public ref class ExpectedExceptionBaseAttribute abstract : public Attribute
[<AbstractClass>]
[<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false, Inherited = true)>]
type ExpectedExceptionBaseAttribute =  
    class 
        inherit Attribute 
    end
public abstract class ExpectedExceptionBaseAttribute extends Attribute

ExpectedExceptionBaseAttribute 類型會公開下列成員。

建構函式

  名稱 描述
受保護的方法 ExpectedExceptionBaseAttribute() 初始化 ExpectedExceptionBaseAttribute 類別的新執行個體。
受保護的方法 ExpectedExceptionBaseAttribute(String) 初始化 ExpectedExceptionBaseAttribute 類別的新執行個體。

回頁首

屬性

  名稱 描述
受保護的屬性 NoExceptionMessage 基礎架構。
受保護的屬性 TestContext 基礎架構。
公用屬性 TypeId 在衍生類別中實作時,取得這個 Attribute 的唯一識別項。 (繼承自 Attribute)。

回頁首

方法

  名稱 描述
公用方法 Equals 基礎架構。 傳回數值,表示這個執行個體是否等於指定的物件。 (繼承自 Attribute)。
受保護的方法 Finalize 在記憶體回收開始前,允許物件嘗試釋放資源,並執行其他清除作業。 (繼承自 Object)。
公用方法 GetHashCode 傳回這個執行個體的雜湊碼。 (繼承自 Attribute)。
公用方法 GetType 取得目前實例 Type 的屬性。 (繼承自 Object)。
公用方法 IsDefaultAttribute 在衍生類別中覆寫時,表示這個執行個體的值是否為衍生類別的預設值。 (繼承自 Attribute)。
公用方法 Match 在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。 (繼承自 Attribute)。
受保護的方法 MemberwiseClone 建立目前 Object 的淺層複製 (Shallow Copy) 。 (繼承自 Object)。
受保護的方法 RethrowIfAssertException 如果它是 AssertFailedExceptionAssertInconclusiveException,則再次擲回例外狀況。
公用方法 ToString 傳回代表目前物件的字串。 (繼承自 Object)。
受保護的方法 Verify 基礎架構。

回頁首

明確介面實作

  名稱 描述
明確介面實作私用方法 System#Runtime#InteropServices#_Attribute#GetIDsOfNames 將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。 (繼承自 Attribute)。
明確介面實作私用方法 System#Runtime#InteropServices#_Attribute#GetTypeInfo 擷取物件的型別資訊,可以用來取得介面的型別資訊。 (繼承自 Attribute)。
明確介面實作私用方法 System#Runtime#InteropServices#_Attribute#GetTypeInfoCount 擷取物件提供的型別資訊介面數目 (0 或 1)。 (繼承自 Attribute)。
明確介面實作私用方法 System#Runtime#InteropServices#_Attribute#Invoke 提供物件所公開的屬性和方法的存取權。 (繼承自 Attribute)。

回頁首

備註

藉由實作您預期的例外狀況驗證,您可以指定 ExpectedExceptionAttribute 類別內建方法無法處理的其他資訊和要求,如下所示:

  • 確認例外狀況的狀態。

  • 預期有一多類型的例外狀況。

  • 當擲回錯誤的例外狀況類型時顯示自訂訊息。

  • 控制負面測試的結果。

  • 如需如何使用屬性的詳細資訊,請參閱使用屬性擴充中繼資料

範例

下列類別包含要測試的方法。

using System;

namespace CSExample
{
    public class DivisionClass
    {
        private int fraction;

        public int Divide(int numerator, int denominator)
        {
            return numerator / denominator;
        }
    }
}

下列自訂屬性類別 (ExpectedArithmeticException) 衍生自 ExpectedExceptionBaseAttribute 類別,以確認例外狀況型別及訊息。 ExpectedArithmeticException 屬性會確認測試方法是否擲回型別為 System.ArithmeticException 或由此衍生的例外狀況。 這也會驗證例外狀況訊息符合指定的例外狀況訊息。 單元測試會通過,因為它會擲回一個衍生自 ArithmeticExceptionDivideByZeroException,而且指定的例外狀況訊息會符合單元測試擲回的例外狀況訊息。

using CSExample;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestProject1
{
    public sealed class ExpectedArithmeticException : ExpectedExceptionBaseAttribute
    {
        private string exceptionMessage;
        private string wrongExceptionMessage;

        public string WrongExceptionMessage
        {
            get
            {
                return wrongExceptionMessage;
            }
            set
            {
                wrongExceptionMessage = value;
            }
        }

        public ExpectedArithmeticException(string expectedExceptionMessage) : this(expectedExceptionMessage, "No exception was thrown.")
        {
        }

        public ExpectedArithmeticException(string expectedExceptionMessage, string noExceptionMessage)
            : base(noExceptionMessage)
        {
            exceptionMessage = expectedExceptionMessage;
            WrongExceptionMessage = "The exception that was thrown does not derive from System.ArithmeticException.";
        }

        protected override void Verify(System.Exception exception)
        {
            Assert.IsNotNull(exception);

            // Handle assertion exceptions from assertion failures in the test method, since we are not interested in verifying those
            base.RethrowIfAssertException(exception);

            Assert.IsInstanceOfType(exception, typeof(System.ArithmeticException), wrongExceptionMessage);
            Assert.AreEqual(exceptionMessage, exception.Message, "Could not verify the exception message.");
        }
    }


    [TestClass()]
    public class DivisionClassTest
    {

        /* This test will pass because it thows a System.DivideByZeroException which derives from System.ArithmeticException. */
        [TestMethod()]
        [ExpectedArithmeticException("Attempted to divide by zero.", "An exception was expected, but no exception was thrown.", WrongExceptionMessage = "The wrong type of exception was thrown.")]
        public void DivideTest()
        {
            DivisionClass target = new DivisionClass();
            int numerator = 5;
            int denominator = 0;
            int actual;
            actual = target.Divide(numerator, denominator);
        }

    }
}

執行緒安全

這個類型的任何 Public static (在 Visual Basic 中為 Shared) 成員都是安全執行緒。不保證任何執行個體成員是安全執行緒。

請參閱

參考

Microsoft.VisualStudio.TestTools.UnitTesting 命名空間