Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 352
Kit Avatar asked Jul 27 '11 13:07

Kit


Video Answer


2 Answers

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

like image 74
Kit Avatar answered Sep 27 '22 21:09

Kit


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.

like image 40
larsmoa Avatar answered Sep 27 '22 19:09

larsmoa