简体   繁体   English

如何使用附加的调试器运行测试时,如何防止VerificationException?

[英]How can I prevent a VerificationException when running a test with attached debugger?

Whenever I run either of the following unit test with a debugger attached, I get a VerificationException inside FluentValidation code at this point (will post whole stacktrace later if necessary): 每当我运行附加调试器的以下任一单元测试时,此时我在FluentValidation代码中得到一个VerificationException (如果需要,稍后将发布整个堆栈跟踪 ):

at FluentValidation.Resources.LocalizedStringSource.CreateFromExpression(Expression`1 expression, IResourceAccessorBuilder resourceProviderSelectionStrategy)
in ...\FluentValidation\Resources\LocalizedStringSource.cs:line 66

The tests are: 测试是:

using FluentValidation;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var c = new MyClass();
        var v = new MyValidator();
        v.Validate(c);
    }

    [TestMethod]
    public void TestMethod2()
    {
        Exception ex = null;
        var done = new ManualResetEvent(false);
        ThreadPool.QueueUserWorkItem(
            o =>
            {
                try
                {
                    TestMethod1();
                }
                catch (Exception e)
                {
                    ex = e;
                }
                finally
                {
                    done.Set();
                }
            });

        done.WaitOne();
        Assert.IsNull(ex);
    }
}

public class MyValidator : AbstractValidator<MyClass>
{
    public MyValidator()
    {
        RuleFor(c => c.MyProperty).GreaterThan(0);
    }
}

public class MyClass
{
    public int MyProperty { get; set; }
}

I've referenced just these assemblies in a single-solution, single-project scenario, targeting the 4.0.30319 runtime: 我在单一解决方案,单项目场景中引用了这些程序集,目标是4.0.30319运行时:

  • FluentValidation v3.0.0.0 FluentValidation v3.0.0.0
  • Microsoft.VisualStudio.QualityTools.UnitTestFramework v10.0.0.0 Microsoft.VisualStudio.QualityTools.UnitTestFramework v10.0.0.0
  • System 系统
  • System.Core System.Core程序

Some other points: 其他一些观点:

  • Running the test without a debugger works fine 没有调试器运行测试工作正常
  • Code coverage is turned off 代码覆盖率已关闭
  • I've minimized the referenced assemblies down to the minimum 我已将引用的组件最小化到最小
  • I don't see any errors in the Fusion log 我没有在Fusion日志中看到任何错误
  • I tried applying the SecurityRulesAttribute from the answer to a similar question 我尝试将答案中SecurityRulesAttribute应用于类似的问题
  • I tried some things from a blog post on VerificationException and testing 我在VerificationException和测试的博客文章中尝试了一些东西
  • Occurs under both MSTest and Resharper hosts (haven't tried NUnit, because the common thread seems to be 'under debugger'. 在MSTest和Resharper主机下发生(没有尝试过NUnit,因为通用线程似乎是'在调试器下'。
  • Occurs when running VS as admin or non-admin 以管理员或非管理员身份运行VS时发生

Does anyone know how I can prevent this VerificationException , work around it, and/or why it's being caused? 有谁知道如何防止这种VerificationException ,解决它,和/或它为什么会被造成? It seems with so few assemblies, there shouldn't be any conflicting ones loading. 似乎有这么少的装配,不应该有任何冲突的装载。 I've also moved the FluentValidation satellite assemblies out of the way but still get the exception. 我还将FluentValidation卫星程序集移开了,但仍然得到了异常。

Ok, I've got it. 好的,我知道了。 First I'd like to acknowledge Jeremy Skinner for working with me to reproduce the problem. 首先,我要感谢Jeremy Skinner 与我一起重现这个问题。 His help spurred me to try tweaking my environment further. 他的帮助促使我尝试进一步调整我的环境。

To prevent the problem you either have to disable IntelliTrace in Visual Studio 2010 Ultimate, or you have to add FluentValidation to the list of modules that IntelliTrace should exclude from collecting data. 要防止出现此问题,您必须在Visual Studio 2010 Ultimate中禁用IntelliTrace ,或者必须将FluentValidation添加到IntelliTrace应从收集数据中排除的模块列表中。 My web searches seem to indicate it's an IntelliTrace bug. 我的网络搜索似乎表明它是一个IntelliTrace错误。 Jim Nakashima in his blog post says: Jim Nakashima在他的博客文章中说:

The issue is that IntelliTrace itself has a bug where methods that have a boolean out parameter in an assembly that is marked as SecurityTransparent will fail when IntelliTrace collection is set to “high” which is the default in the Cloud IntelliTrace scenario. 问题是IntelliTrace本身有一个错误,当IntelliTrace集合设置为“高”(这是Cloud IntelliTrace场景中的默认值)时,标记为SecurityTransparent的程序集中具有boolean out参数的方法将失败。

You will see this in your own code if you have a method whose signature includes a boolean out parameter and you have set your assembly security to SecurityTransparent. 如果您的方法的签名包含布尔输出参数并且您已将程序集安全性设置为SecurityTransparent,您将在自己的代码中看到这一点。

I looked at my stack trace and briefly through the FluentValidation source, but didn't see this. 我查看了我的堆栈跟踪并简要介绍了FluentValidation源代码,但没有看到这一点。 I suspect it might be a similar IntelliTrace instrumentation bug relating to LINQ expressions. 我怀疑它可能是与LINQ表达式相关的类似IntelliTrace检测错误。

Anyway, here's how to fix the issue: 无论如何,这是如何解决问题:

  1. In VS, select Debug | 在VS中,选择Debug | Options And Settings... | 选项和设置... | IntelliTrace | IntelliTrace | Modules 模块
  2. In the following dialog, click Add... and enter FluentValidation into the text box. 在以下对话框中,单击“ 添加...” ,然后在文本框中输入FluentValidation。

在此输入图像描述

I experienced the same issue and found TypeMock 6.0 to be the culprit. 我遇到了同样的问题,发现TypeMock 6.0是罪魁祸首。 By disabling TypeMock Isolator (menu TypeMock -> Disable TypeMock Isolator) I got rid of the problem. 通过禁用TypeMock Isolator(菜单TypeMock - >禁用TypeMock Isolator)我摆脱了问题。 This of course breaks any TypeMock dependent test. 这当然会破坏任何与TypeMock相关的测试。

Note that adding FluentValidation to IntelliTrace exceptions does not resolve the issue when TypeMock is the problem. 请注意,当TypeMock出现问题时,将FluentValidation添加到IntelliTrace异常并不能解决问题。

In my case my Asp.net MVC 3 application had a reference to FluentValidation.dll and FluentValidation.mvc.dll files. 在我的情况下,我的Asp.net MVC 3应用程序引用了FluentValidation.dll和FluentValidation.mvc.dll文件。

I removed the references and installed the FluentValidation for MVC 3 using nuget package manager and It worked. 我删除了引用并使用nuget包管理器安装了FluentValidation for MVC 3并且它工作正常。

It downloaded FluentValidation.Mvc version 5.0.0.1 它下载了FluentValidation.Mvc版本5.0.0.1

暂无
暂无

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

相关问题 如何在抛出异常时阻止调试器进入我的方法? - How can I prevent the debugger from entering my method when Exception is thrown? 在Mono中运行时如何防止引用某个程序集 - How can I prevent the referencing of a certain assembly when running in Mono 如何集成测试附加到 Visual Studio 调试器的控制台应用程序 - How to ingration test Console App attached to Visual Studio debugger 如何检测我的应用程序在带有调试器的 vs2008 中运行? - How to detect that my application is running in vs2008 with debugger attached? 为什么在.NET 4下运行时此行会导致VerificationException? - Why does this line cause a VerificationException when running under .NET 4? 未连接调试器时,FolderPicker崩溃 - FolderPicker crashes when not attached to debugger 访问冲突异常仅在未附加调试器的情况下运行C#app时出现 - Access Violation Exception only appearing when running C# app without debugger attached ILGenerator:如何使用非托管指针? (我得到一个验证异常) - ILGenerator: How to use unmanaged pointers? (I get a VerificationException) 遇到断点时,如何防止Windows.Forms OnDeactivate中的代码运行? - How can I prevent code in Windows.Forms OnDeactivate running when hitting a breakpoint? 从 .NET 库中,如何检查 RUNNING 程序集/应用程序是否处于调试版本(未附加调试器)? - From a .NET library, how to check if the RUNNING assembly/app is in Debug build (without Debugger attached)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM