运行时类型而不是C#单元测试中的类型
在我的一个单元测试中,我想检查是否所有公共方法都返回ActionResult类型。以下是我的测试方法:运行时类型而不是C#单元测试中的类型,c#,.net,asp.net-mvc,unit-testing,reflection,C#,.net,Asp.net Mvc,Unit Testing,Reflection,在我的一个单元测试中,我想检查是否所有公共方法都返回ActionResult类型。以下是我的测试方法: [TestMethod] public void Public_Methods_Should_Only_Return_ActionResults() { MethodInfo[] methodInfos = typeof(MyController).GetMethods(BindingFlags.Public | BindingFlags.Instan
[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>.
Assert.IsInstanceOfType失败。所需类型:。实际类型:。
当我在该断言上设置断点并检查methodInfo.ReturnType时,它的类型是type,它的类型是ActionResult
有人能解释一下为什么考试会失败,以及如何让考试成功吗
提前感谢,,
MR使用Assert.AreEqual代替Assert.IsInstanceOfType。您希望检查结果的类型,而不是反映的类型信息的类型