Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.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#_Unit Testing_Private - Fatal编程技术网

单元测试静态方法C#程序

单元测试静态方法C#程序,c#,unit-testing,private,C#,Unit Testing,Private,我想对主程序的静态方法进行单元测试: public class Program { public static int Main(string[] args) { ... } private static bool TheAnswer(int ans) { if (ans == 42) return true; return false; } } 为了测试它,在我的单元测试项目中,我有: var

我想对主程序的静态方法进行单元测试:

public class Program
{
    public static int Main(string[] args)
    { ... }
    private static bool TheAnswer(int ans)
    { 
        if (ans == 42)
            return true;
        return false;
     }
}
为了测试它,在我的单元测试项目中,我有:

var t = new PrivateType(typeof(Program));
bool result = Convert.ToBoolean(t.InvokeStatic("TheAnswer", 42));
Assert.AreEqual(result, true);
但是调用InvokeStatic时这是失败的

中发生“System.MissingMethodException”类型的异常 mscorlib.dll,但未在用户代码中处理

其他信息:试图访问缺少的成员

我如何才能使这项工作(我可以吗?)

堆栈跟踪: 'vstest.executionengine.x86.exe'(CLR v4.0.30319:UnitTestAdapter:Running test):加载'C:\PROGRAM FILES(x86)\MICROSOFT VISUAL STUDIO 14.0\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW\Extensions\MICROSOFT.VisualStudio.TestPlatform.Extensions.VSTestIntegration.dll'。已跳过加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。 'vstest.executionengine.x86.exe'(CLR v4.0.30319:UnitTestAdapter:Running test):加载了'C:\Windows\assembly\GAC\MSIL\Microsoft.VisualStudio.QualityTools.UnitTestFramework\10.0.0\Uuu b03f5f7f11d50a3a\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll'。已跳过加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。 'vstest.executionengine.x86.exe'(CLR v4.0.30319:UnitTestAdapter:Running test):加载'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0\U 4.0.0.0\UU b77a5c561934e089\System.Data.dll'。已跳过加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。 'vstest.executionengine.x86.exe'(CLR v4.0.30319:UnitTestAdapter:Running test):加载'C:\Projects\myprogram\test\u the \u test\bin\Debug\test\u the \u test.dll'。已加载符号。 'vstest.executionengine.x86.exe'(CLR v4.0.30319:UnitTestAdapter:Running test):加载'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0.0.0_b77a5c561934e089\System.Transactions.dll'。已跳过加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。 “vstest.executionengine.x86.exe”(CLR v4.0.30319:UnitTestAdapter:Running test):加载了“C:\Windows\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0.0.0_b03f5f7f11d50a3a\System.EnterpriseServices.dll”。已跳过加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。 'vstest.executionengine.x86.exe'(CLR v4.0.30319:UnitTestAdapter:Running test):加载'C:\Windows\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0.0.0_b03f5f7f11d50a3a\System.EnterpriseServices.Wrapper.dll'。已跳过加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。 'vstest.executionengine.x86.exe'(CLR v4.0.30319:UnitTestAdapter:Running test):加载'C:\Projects\myprogram\test\u the\u test\bin\Debug\myprogram.exe'。已加载符号。 引发异常:mscorlib.dll中的“System.MissingMethodException” 'vstest.executionengine.x86.exe'(CLR v4.0.30319:UnitTestAdapter:Running test):加载'C:\Windows\assembly\GAC\U MSIL\Microsoft.VisualStudio.Debugger.Runtime\14.0.0.0\uu b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'。已跳过加载符号。模块已优化,调试器选项“仅我的代码”已启用。

请尝试以下操作:

    object[] args = new object[1] { 42 };
    bool result = (bool)typeof(Program).InvokeMember(
                    name: "TheAnswer", 
                    invokeAttr: BindingFlags.NonPublic |
                                BindingFlags.Static |
                                BindingFlags.InvokeMethod,
                    binder: null, 
                    target: null, 
                    args: args);
Assert.AreEqual(result, true);

你应该问问自己测试私有方法是否合乎逻辑。根据定义,我认为你应该只测试公共方法。问问自己,测试私有方法是否有意义。如果是的话,把它公之于众可能是个好主意。为什么不直接把它公之于众并直接打电话给它呢?你的理由是什么?显而易见的解决办法是公开。这是一个可怕的理由。我可以通过学习如何把竹笋放在我的指甲下或射自己的脚来“扩展我的知识”。但这没有意义。正如多次向您指出的,您不应该对私有方法进行单元测试。那样做没有意义。学习实际有用的好东西。不要学着做一些不好的事情。仍然有一个缺少成员的例外