Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行时类型而不是C#单元测试中的类型_C#_.net_Asp.net Mvc_Unit Testing_Reflection - Fatal编程技术网

运行时类型而不是C#单元测试中的类型

运行时类型而不是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

在我的一个单元测试中,我想检查是否所有公共方法都返回ActionResult类型。以下是我的测试方法:

    [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。您希望检查结果的类型,而不是反映的类型信息的类型