मैं अगले समारोह के लिए एक इकाई परीक्षण लिखने के लिए की जरूरत है और मैंने देखा कि मैं उपयोग कर सकते हैं [ExpectedException]ExpectedException जोर
इस समारोह का परीक्षण किया जा रहा है। फेंक दिया जा करने के लिए
public static T FailIfEnumIsNotDefined<T>(this T enumValue, string message = null)
where T:struct
{
var enumType = typeof (T);
if (!enumType.IsEnum)
{
throw new ArgumentOutOfRangeException(string.Format("Type {0} is not an Enum, therefore it cannot be checked if it is Defined not have defined.", enumType.FullName));
}
else if (!Enum.IsDefined(enumType, enumValue))
{
throw new ArgumentOutOfRangeException(string.Format("{1} Value {0} is not does not have defined value in Enum of type {0}. It should not be...", enumType.FullName, message ?? ""));
}
return enumValue;
}
और यहाँ अपवाद है कि अपेक्षा की जाती है परीक्षण करने के लिए कोड जाना होगा
[TestMethod]
[ExpectedException(ArgumentOutOfRangeException(ArgumentException), "message")]
public void FailIfEnumIsNotDefined_Check_That_The_Value_Is_Not_Enum()
{
// PREPARE
// EXECUTE
// ASSERT
}
मैं विचार नहीं है या तो अपवाद के लिए ज़ोर बनाने के लिए किया है।
उपयोग 'Assert.Throws()' 'बजाय ExpectedException' की क्योंकि यह विशेषता अगर exc परीक्षण पारित करता है: जहां बिल्कुल अपवाद उत्पन्न करने के लिए किया जा उम्मीद है परीक्षण विधि कोड के किसी भी हिस्से में उत्सर्जन हुआ। 'Assert.Throws' कोड की सटीक जगह का परीक्षण करने की अनुमति देता है जहां अपवाद होता है। – sthotakura
@sthotakura MSTest में Assert नहीं है। जब तक आप एक कस्टम लिखते हैं, तब तक वांछित एमएसटीएस्ट अपवाद हैंडलिंग कौन सा है। NUnit के समान :) मेरे उत्तर में एक लिंक है – Spock
@Spock मुझे यह नहीं पता था, धन्यवाद! – sthotakura