2015-03-08 70 views
0

除了我的Assert之外,一切都在MSUnit上進行,我檢查並發現MSUnit與nUnit有點不同,它經歷了一篇博客文章,完全一樣,但在調試測試時仍然出現錯誤。MSUnit Assert not Working

當我檢查我在單元測試EX(引發錯誤後),我得到的消息是「錯誤的應用程序」,其中未通過測試的預期消息是不同

什麼想法?

[TestMethod] 
    [ExpectedException(typeof(InvalidAreaException))] 
    public void GetAreaWithNegativeValueTest() 
    { 
     try 
     { 
      Utility.GetArea(2, -1, 2); 

     } 
     catch (InvalidAreaException ex) 
     { 
      Assert.AreEqual ("Inputs must be positive numbers", ex.Message); 
      throw; 
     } 
    } 

//Exception Class 
     public class InvalidAreaException : ApplicationException, ISerializable 
    { 
    public string msg; 

    public InvalidAreaException(string message) 
    { 
     //msg = message; 
    } 
} 

//Actual Method to be tested 
public static double GetArea(int arg1, int arg2, int arg3) 

    { 
     double area = 0d; 

     if ((arg1 < 0) || (arg2 < 0) || (arg3 < 0)) 
     { 
      throw new InvalidAreaException("Inputs must be positive numbers"); 
     } 
    } 
+0

你的GetArea方法沒有返回語句,這甚至編譯? – Paolo 2015-03-08 17:35:34

+0

它不叫MSUnit它被稱爲MSTests框架,無論如何嘗試從方法中刪除try/catch:「GetAreaWithNegativeValueTest」,因爲你想方法來檢測異常的權利? – 2015-03-08 17:35:39

+0

我會檢查你的單元測試。一個好的測試應該只有一條路徑通過代碼(否則你冒着測試本身的邏輯問題)。這樣做可能會指出你的問題。 – 2015-03-08 17:36:41

回答

0

變化InvalidAreaException到C'tor:然後你的測試將通過

public class InvalidAreaException : ApplicationException, ISerializable 
    { 

     public InvalidAreaException(string message):base(message) 
     { 
     } 
    } 

你的斷言是對信息屬性,它是基類的屬性,你忘了填充這個屬性