Question

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. I am using xUnit version 1.9 on a 64-bit machine.

To run tests from the command line, I am using the following command:

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.

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. 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)

It looks like the exception is coming from xUnit; however, I am able to run the tests using xunit.console.clr4.exe alone without issue. It only fails when using the xUnit console from the Moles runner.

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.

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.

Update: I decided to download the code from http://xunit.codeplex.com/SourceControl/changeset/changes/600246119dca and debug myself. 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:

[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). 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. 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.

Was it helpful?

Solution

Compiling from the latest source code from CodePlex runs without the MethodAccessException.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top