2009-07-30 29 views
7

在我的一個單元測試中,我想檢查所有公共方法是否返回ActionResult類型。這裏是我的測試方法:RuntimeType而不是C#中的Type單元測試

[TestMethod] 
    public void Public_Methods_Should_Only_Return_ActionResults() 
    { 

     MethodInfo[] methodInfos = typeof(MyController).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); 

     foreach (MethodInfo methodInfo in methodInfos) 
     { 
      Assert.IsInstanceOfType(methodInfo.ReturnType, typeof(System.Web.Mvc.ActionResult)); 

     } 

    } 

該測試炸燬從myController的第一種方法:

[Authorize] 
public ActionResult MyList() 
{ 
    return View(); 
} 

,出現以下錯誤:

Assert.IsInstanceOfType failed. Expected type:<System.Web.Mvc.ActionResult>. Actual type:<System.RuntimeType>. 

當我設置一個斷點上斷言並檢查methodInfo.ReturnType它是Type類型,它是ActionResult。

任何人都可以解釋爲什麼測試是炸燬,做什麼使它工作?

由於提前, MR

回答

11

使用Assert.AreEqual代替Assert.IsInstanceOfType。您想檢查結果的類型,而不是反映類型信息的類型。

+0

非常感謝。現在它像我想要的那樣工作。 – 2009-07-30 21:38:31