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

发布于 2024-11-26 17:36:16 字数 2448 浏览 0 评论 0 原文

每当我在附加调试器的情况下运行以下任一单元测试时,我都会在 FluentValidation 内收到 VerificationException此时的代码(如有必要,稍后将发布整个堆栈跟踪):

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

测试是:

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; }
}

我在单一解决方案、单一项目场景中仅引用了这些程序集,目标是 4.0.30319运行时:

  • FluentValidation v3.0.0.0
  • Microsoft.VisualStudio.QualityTools.UnitTestFramework v10.0.0.0
  • System
  • System.Core

其他一些要点:

有谁知道如何防止此 VerificationException、解决它和/或为什么会导致它?看来程序集这么少,不应该加载任何冲突的程序集。我还将 FluentValidation 卫星程序集移开,但仍然遇到异常。

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

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:

  • FluentValidation v3.0.0.0
  • Microsoft.VisualStudio.QualityTools.UnitTestFramework v10.0.0.0
  • System
  • 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
  • I tried applying the SecurityRulesAttribute from the answer to a similar question
  • I tried some things from a blog post on VerificationException and testing
  • Occurs under both MSTest and Resharper hosts (haven't tried NUnit, because the common thread seems to be 'under debugger'.
  • Occurs when running VS as admin or non-admin

Does anyone know how I can prevent this VerificationException, work around it, and/or why it's being caused? 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

套路撩心 2024-12-03 17:36:16

好的,我知道了。首先,我要感谢 Jeremy Skinner 用于使用我重现该问题。他的帮助激励我尝试进一步调整我的环境。

为了防止出现此问题,您必须在 Visual Studio 2010 Ultimate 中禁用 IntelliTrace ,或者您必须将 FluentValidation 添加到 IntelliTrace 应排除的模块列表中收集数据。我的网络搜索似乎表明这是一个 IntelliTrace 错误。 Jim Nakashima 在他的 博客文章 说:

问题在于 IntelliTrace 本身存在一个错误,当 IntelliTrace 集合设置为“高”(云 IntelliTrace 场景中的默认设置)时,在标记为 SecurityTransparent 的程序集中具有布尔输出参数的方法将会失败。< /p>

如果您的方法的签名包含布尔输出参数,并且您已将程序集安全性设置为 SecurityTransparent,那么您将在自己的代码中看到这一点。

我查看了堆栈跟踪并简要浏览了 FluentValidation 源代码,但没有看到这一点。我怀疑这可能是与 LINQ 表达式相关的类似 IntelliTrace 检测错误。

不管怎样,解决这个问题的方法如下:

  1. 在 VS 中,选择调试 | 选项和设置... | IntelliTrace | 模块
  2. 在接下来的对话框中,单击添加...,然后在文本框中输入 FluentValidation。

在此处输入图像描述

Ok, I've got it. First I'd like to acknowledge Jeremy Skinner for working with me to reproduce the problem. 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. My web searches seem to indicate it's an IntelliTrace bug. Jim Nakashima in his blog post says:

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.

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.

I looked at my stack trace and briefly through the FluentValidation source, but didn't see this. I suspect it might be a similar IntelliTrace instrumentation bug relating to LINQ expressions.

Anyway, here's how to fix the issue:

  1. In VS, select Debug | Options And Settings... | IntelliTrace | Modules
  2. In the following dialog, click Add... and enter FluentValidation into the text box.

enter image description here

蓝天白云 2024-12-03 17:36:16

我遇到了同样的问题,并发现 TypeMock 6.0 是罪魁祸首。通过禁用 TypeMock Isolator(菜单 TypeMock -> 禁用 TypeMock Isolator),我解决了这个问题。这当然会破坏任何 TypeMock 相关测试。

请注意,当 TypeMock 出现问题时,将 FluentValidation 添加到 IntelliTrace 异常并不能解决问题。

I experienced the same issue and found TypeMock 6.0 to be the culprit. By disabling TypeMock Isolator (menu TypeMock -> Disable TypeMock Isolator) I got rid of the problem. This of course breaks any TypeMock dependent test.

Note that adding FluentValidation to IntelliTrace exceptions does not resolve the issue when TypeMock is the problem.

暮光沉寂 2024-12-03 17:36:16

就我而言,我的 Asp.net MVC 3 应用程序引用了 FluentValidation.dll 和 FluentValidation.mvc.dll 文件。

我删除了引用并使用 nuget 包管理器安装了适用于 MVC 3 的 FluentValidation,并且它有效。

它下载了 FluentValidation.Mvc 版本 5.0.0.1

In my case my Asp.net MVC 3 application had a reference to FluentValidation.dll and FluentValidation.mvc.dll files.

I removed the references and installed the FluentValidation for MVC 3 using nuget package manager and It worked.

It downloaded FluentValidation.Mvc version 5.0.0.1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文