运行单元测试包含Microsoft.Fakes通过反射

时间:2016-03-03 10:44:22

标签: c# unit-testing reflection appdomain microsoft-fakes

我正在尝试生成自己的Test Runner,它通过反射访问单元测试方法,并在单独的AppDomain中运行它们。

object instance = Activator.CreateInstance(targetType);

foreach (MethodInfo method in methods)
{
    method.Invoke(instance, null);
}

这很有效,直到其中一个测试使用Microsoft.Fakes,此时方法尝试创建ShimsContext时会抛出此异常:

System.Reflection.TargetInvocationException was unhandled
  HResult=-2146232828
  Message=Exception has been thrown by the target of an invocation.
  Source=mscorlib
  StackTrace:
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       ...
  InnerException: 
       HResult=-2146233088
       Message=Failed to resolve profiler path from COR_PROFILER_PATH and COR_PROFILER environment variables.
       Source=Microsoft.QualityTools.Testing.Fakes
       StackTrace:
            at Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.IntelliTraceInstrumentationProvider.ResolveProfilerPath()
            at Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.IntelliTraceInstrumentationProvider.Initialize()
            at Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.InitializeUnitTestIsolationInstrumentationProvider()
            at Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.CreateContext()
            at Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create()
            at UnitTests.TestRunnerTests.Builders.AppDomainBuilderTests.AppDomainBuilderBuildsAppDomainsWithPermissionStateNone() in c:\VS\UnitTests\TestRunnerTests\Builders\AppDomainBuilderTests.cs:line 130
       InnerException:

在尝试运行此测试时,我能够成功调用TestInitialise方法,但它会在TestMethod中抛出此异常,如下所示......

/// <summary>
/// A test to ensure that the Application domain builder builds an 
/// application domains with permission state none.
/// </summary>
[TestMethod]
public void AppDomainBuilderBuildsAppDomainsWithPermissionStateNone()
{
    // Arrange
    using (ShimsContext.Create())
    {
        bool wasCalledCorrectly = false;

        PermissionState expected = PermissionState.None;

        ShimPermissionSet.ConstructorPermissionState = (permisionSet, actual) =>
        {
            if (expected == actual)
            {
                wasCalledCorrectly = true;
            }
        };

        AppDomainBuilder target = new AppDomainBuilder();

        // Act
        target.Build(this.defaultInput);

        // Assert
        Assert.IsTrue(wasCalledCorrectly);
    }
}

查看了异常详细信息后,似乎是因为测试无法通过环境变量访问Intellisense Profiler。我想知道是否有人有任何将此对象注入AppDomain的经验,以便可以成功生成Fakes?

或者我是否需要在AppDomain中包含VSTest.ExecutionEngine并以某种方式调用它?

任何有关这方面的帮助都将受到高度赞赏,因为能够支持Microsoft.Fakes而无需呼叫VSTest.Console.Exe将是非常需要的。

由于

0 个答案:

没有答案