Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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# 通过反射运行包含Microsoft.Fakes的单元测试_C#_Unit Testing_Reflection_Appdomain_Microsoft Fakes - Fatal编程技术网

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

C# 通过反射运行包含Microsoft.Fakes的单元测试,c#,unit-testing,reflection,appdomain,microsoft-fakes,C#,Unit Testing,Reflection,Appdomain,Microsoft Fakes,我正在尝试生成自己的测试运行程序,它通过反射访问单元测试方法,并在单独的AppDomain中运行它们 object instance = Activator.CreateInstance(targetType); foreach (MethodInfo method in methods) { method.Invoke(instance, null); } 在其中一个测试使用Microsoft.Fakes之前,此操作一直非常有效。在该方法尝试创建ShimsContext时,会引发以

我正在尝试生成自己的测试运行程序,它通过反射访问单元测试方法,并在单独的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:
在尝试运行此测试时,我能够成功调用TestInitialize方法,但它在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);
    }
}
//
///确保应用程序域生成器生成
///权限状态为“无”的应用程序域。
/// 
[测试方法]
public void AppDomainBuilderBuildsAppDomainsWithPermissionStateNone()
{
//安排
使用(ShimsContext.Create())
{
bool wastcalledcorrectly=false;
PermissionState预期=PermissionState.None;
ShimPermissionSet.ConstructorPermissionState=(PermissionSet,实际)=>
{
如果(预期==实际)
{
wastCalledCorrectly=true;
}
};
AppDomainBuilder目标=新建AppDomainBuilder();
//表演
target.Build(this.defaultInput);
//断言
Assert.IsTrue(已正确调用);
}
}
查看了异常详细信息后,似乎是因为测试无法通过环境变量访问Intellisense Profiler。我想知道是否有人有经验将这个对象注入AppDomain以便成功生成假货

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

如果您能帮助我们了解这一点,我们将不胜感激,因为我们能够支持Microsoft。不需要调用VSTest.Console.Exe即可创建Fakes

谢谢