2009-07-30 11 views
7

私のユニットテストの1つでは、すべてのパブリックメソッドがActionResultのタイプを返すかどうかをチェックしたいと思います。私は主張にブレークポイントを設定しC#単位のタイプの代わりにRuntimeTypeユニットテスト

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

:次のエラーで

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

[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からの第一の方法で吹く:ここに私のテスト方法ですmethodInfo.ReturnTypeをチェックします。これはType型で、ActionResultです。

誰かがなぜテストが爆発し、それを動作させるために何をすべきか説明できますか?事前に

おかげで、代わりにAssert.IsInstanceOfTypeの MR

答えて

11

使用Assert.AreEqual。反射型情報のタイプではなく、結果のタイプをチェックします。

+0

ありがとうございます。今、私はそれが欲しいのと同じように動作します。 –

関連する問題