c# - 单元测试隔离异常 : Throws Exception while running Shims test in Release/Debug mode

标签 c# unit-testing microsoft-fakes

我正在尝试使用 Microsoft Shims 运行此单元测试,但它在 Shims.Context.Create(); 方法中抛出异常。

环境:VS 2012,Win2K8 R2

namespace MyShimsUnitTest
{
    [TestClass]
    public class MyUnitTest
    {
        [TestMethod]
        public void GetCurrentYear()
        {
            using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create())
            {
                // Some Logic...
            }
        }

    }
}

详细异常:

Result Message: 
Test method MyShimsUnitTest.MyUnitTest.GetCurrentYear threw exception: 
Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationException: UnitTestIsolation instrumentation failed to initialize. Please restart Visual Studio and rerun this test
Result StackTrace:  
at Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.InitializeUnitTestIsolationInstrumentationProvider()
   at Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.CreateContext()
   at Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create()
   at MyShimsUnitTest.MyUnitTest.GetCurrentYear()

最佳答案

垫片需要由 IntelliTrace 探查器执行的运行时检测。测试运行器负责设置 CLR 加载探查器所需的环境变量,并提供探查器必须为 Shims 检测的类型列表。当 ShimRuntime 无法找到并附加到 IntelliTrace 探查器时,将抛出 UnitTestIsolationException,它预计 CLR 已经加载了它。

正如 Jin-Wook 之前提到的,当测试由未执行所需的探查器初始化的运行器执行时,就会出现此问题。 Test Explorer 和 vstest.console.exe 是 Visual Studio 2012 附带的两个这样的运行器。此时,Visual Studio 测试运行器在以“传统”模式运行测试时不执行所需的探查器检测,这发生在您有为您的运行选择的 .TESTSETTINGS 文件或强制使用传统模式的 .RUNSETTINGS 文件。

您可以使用支持 Shims 所需的分析器检测的第三方测试运行器。

关于c# - 单元测试隔离异常 : Throws Exception while running Shims test in Release/Debug mode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16664814/

相关文章:

c# - 字符串有多少个参数

c# - 如何读取给定目标中包含主文件的所有子目录

python - 如何在 Python 模拟中调用模拟方法

c# - 如何使用 Microsoft Fakes 获取基类的垫片?

unit-testing - 哪些方法绝对需要单元测试

c# - 使用 Automapper 映射只读字段

c# - 如何使用带有自定义路由事件的用户控件在 Caliburn Micro 中传递参数?

unit-testing - karma 覆盖总是空的

PHPUnit 没有提供足够的关于跳过和不完整测试的信息

unit-testing - MS Fakes 的 TDD