简体   繁体   English

使用Moles和xUnit.net运行单元测试的未处理的MethodAccessException

[英]Unhandled MethodAccessException using Moles and xUnit.net to run unit tests

I have a unit test project in Visual Studio 2010 (.NET 4) that utilizes the xUnit.net testing framework, Moq, and the Moles Isolation framework for generating stubs of static methods. 我在Visual Studio 2010(.NET 4)中有一个单元测试项目,该项目利用xUnit.net测试框架Moq和Moles Isolation框架生成静态方法的存根。 I am using xUnit version 1.9 on a 64-bit machine. 我在64位计算机上使用xUnit版本1.9。

To run tests from the command line, I am using the following command: 要从命令行运行测试,我使用以下命令:

moles.runner.exe Project.Tests.dll /runner:xunit.console.clr4.exe moles.runner.exe Project.Tests.dll /runner:xunit.console.clr4.exe

However, I get the following exception every time: 但是,每次都会收到以下异常:

instrumenting...started xUnit.net console test runner (64-bit .NET 4.0.30319.1) Copyright (C) 2007-11 Microsoft Corporation. 检测...已启动xUnit.net控制台测试运行程序(64位.NET 4.0.30319.1)版权所有(C)2007-11 Microsoft Corporation。

Unhandled Exception: System.MethodAccessException: Attempt by security transparent method 'Xunit.ConsoleClient.Program.Main(System.String[])' to access security critical method 'System.AppDomain.add_UnhandledException(System.UnhandledExceptionEventHandler)' failed. 未处理的异常:System.MethodAccessException:尝试由安全透明方法'Xunit.ConsoleClient.Program.Main(System.String [])'访问安全关键方法'System.AppDomain.add_UnhandledException(System.UnhandledExceptionEventHandler)'失败。 at Xunit.ConsoleClient.Program.Main(String[] args) at Microsoft.Moles.Runner.MolesRunner.RunnerRunner.Run(String runner, String[] args) at Microsoft.Moles.Runner.MolesRunner.RunnerRunner.Run(String runner, String[] args) at Microsoft.Moles.Runner.MolesRunner.LaunchRunnerEntryPoint(MolesRunnerOptions options) at Microsoft.Moles.Runner.MolesRunner.RunnerMain(String[] args) at Microsoft.Moles.Runner.Program.Main(String[] args) 在Xunit.ConsoleClient.Program.Main(String [] args)在Microsoft.Moles.Runner.MolesRunner.RunnerRunner.Run(字符串赛跑者,String [] args)在Microsoft.Moles.Runner.MolesRunner.RunnerRunner.Run(字符串赛跑者) ,Microsoft.Moles.Runner.Program.Main(String []处的Microsoft.Moles.Runner.MolesRunner.LaunchRunnerEntryPoint(MolesRunnerOptions选项)处的String [] args) args)

It looks like the exception is coming from xUnit; 看起来异常来自xUnit; however, I am able to run the tests using xunit.console.clr4.exe alone without issue. 但是,我可以单独使用xunit.console.clr4.exe运行测试,而不会出现问题。 It only fails when using the xUnit console from the Moles runner. 仅当使用Moles运行程序中的xUnit控制台时,它才会失败。

I found this on a forum post: 我在论坛帖子中找到了这个:

In the .NET 4 framework, security tranparency rules prevent any security transparent code from calling into security critical code. 在.NET 4框架中,安全透明性规则可防止任何安全透明代码调用到安全关键代码中。

What can I check to determine the cause of this error? 我可以检查什么以确定此错误的原因? Is there a security setting I need to change to prevent this? 是否需要更改安全设置以防止这种情况?

Note: I am also having the same exact problem on a 32-bit workstation. 注意:我在32位工作站上也遇到同样的问题。

Update: I decided to download the code from http://xunit.codeplex.com/SourceControl/changeset/changes/600246119dca and debug myself. 更新:我决定从http://xunit.codeplex.com/SourceControl/changeset/changes/600246119dca下载代码并进行调试。 In the xunit.console project (the output of which is the exe getting invoked from the Moles runner), the main thread of execution looks like this: 在xunit.console项目中(其输出是从Moles运行器中调用的exe),执行的主线程如下所示:

[STAThread]
public static int Main(string[] args)
{
    Console.WriteLine("xUnit.net console test runner ({0}-bit .NET {1})", 
        IntPtr.Size * 8, Environment.Version);
    Console.WriteLine("Copyright (C) 2007-11 Microsoft Corporation.");

    if (args.Length == 0 || args[0] == "/?")
    {
        PrintUsage();
        return -1;
    }

    AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

    try
    {
        CommandLine commandLine = CommandLine.Parse(args);

        int failCount = RunProject(commandLine.Project, 
            commandLine.TeamCity, commandLine.Silent);

        if (commandLine.Wait)
        {
            Console.WriteLine();
            Console.Write("Press any key to continue...");
            Console.ReadKey();
            Console.WriteLine();
        }

        return failCount;
    }
    catch (ArgumentException ex)
    {
        Console.WriteLine();
        Console.WriteLine("error: {0}", ex.Message);
        return -1;
    }
    catch (BadImageFormatException ex)
    {
        Console.WriteLine();
        Console.WriteLine("{0}", ex.Message);
        return -1;
    }
}

When I run my tests while debugging the code, everything works fine, as expected (since I never had issues running tests from xUnit alone). 当我在调试代码的同时运行测试时,一切正常,如预期的那样(因为我从来没有遇到过仅从xUnit运行测试的问题)。 I noticed the following line, which appears to be where the exception is thrown based on the error message and stack trace from my original post: 我注意到以下行,该行似乎是基于错误消息和原始帖子中的堆栈跟踪引发异常的地方:

AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

I commented out this line, built xunit.console.exe , and tried using it as the /runner argument when executing the Moles runner again. 我注释掉了这一行,构建了xunit.console.exe ,并在再次执行Moles运行程序时尝试将其用作/ runner参数。 This time, no exception was thrown. 这次,没有异常被抛出。

I am still at a loss as to why a Security exception is being thrown on this line when invoked from moles.runner.exe, but not when I run the xUnit console by itself. 关于为什么从moles.runner.exe调用时为什么会在此行引发安全异常,但我自己运行xUnit控制台时却不知道为什么会在此行引发安全异常,我还是一头雾水。

使用CodePlex的最新源代码进行编译时,无需运行MethodAccessException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM