Question

I'm just trying to get familiar with the new Fakes Isolation Framework in Visual Studio 2012 RC but I'm consequently facing issues with ShimNotSupportedExceptions.
At the first tries, each single shim method I tried to hook up a delegate to, had thrown a ShimNotSupportedException when trying to run/debug the test.

[TestMethod]
public void GetFoo_ValidBar_ReturnsBaz()
{
    using(ShimsContext.Create())
    {
        ShimDateTime.NowGet = () => new DateTime(2012,08,11,10,20,59);

        const string expected = "20120811_102059";
        string actual = GetFoo();

        Assert.AreEqual(expected,actual);
    }
} 

This is the corresponding stack trace:

The GetFoo_ValidBar_ReturnsBaz test method has thrown an exception: Microsoft.QualityTools.Testing.Fakes.Shims.ShimNotSupportedException: System.DateTime at Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.InvokeEvent(T value, Action1 eh) at Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.OnAttachedUnsupportedMethod(MethodBase method) at Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.CheckInstrumentation(MethodBase method) at Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.InternalAttachDetour(Object optionalReceiver, MethodBase method, Delegate detourDelegate) at Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.AttachDetour(Object optionalReceiver, MethodBase method, Delegate detourDelegate) at Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.SetShimMethod(Delegate optionalStub, Object optionalReceiver, MethodBase method) at Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.SetShim(Delegate optionalStub, Type receiverType, Object optionalReceiver, String name, ShimBinding flags, Type returnType, Type[] parameterTypes) at Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.SetShimPublicStatic(Delegate optionalStub, Type receiverType, String name, Type returnType, Type[] parameterTypes) at System.Fakes.ShimDateTime.set_NowGet(Func'1 value) at GetFoo_ValidBar_ReturnsBaz() in BazTests.cs: line 48.

After having read the two threads I had found at MSDN dealing with this issue I followed their instructions (turning CodeCoverage off, deleting .testsettings file) which didn't work for me!
Nevertheless I have found a workaround for this issue:
By firstly running all tests from the Test Explorer (instead of using the "MSTest Test (click to run)" button directly out of the coding area) everything worked correctly and no exceptions were thrown. Afterwards I could even debug the test and the assignment to the shim method worked just as expected.
This worked for all following shims I used as well.
But now I'm having the same issue again when trying to implement fakes of the MS Enterprise Library for database access.

This is what the test looks like:

[TestMethod]
public void GetFooFromEF_NonEmptyDataReader_ObjectsCorrectlyInstantiated()
{
    using(ShimsContext.Create()){
        var dataReader = new StubIDataReader()
            {
                ItemGetString = s => 1,
                DepthGet = () => 2
            };

        ShimFoo.GetBar = guid => dataReader;

        var bar = new StubIBar()
        {
            ConvertIBarToBaz = record => null
        };

        ShimQux.AllInstances.GetBar = (a, b) => bar;

        var dbFactory = new StubDbProviderFactory();
        var db = new StubDatabase("test", dbFactory);
        ShimDatabaseFactory.CreateDatabaseString = s => db;

        List<BarInformation> actual = accessor.InvokeStatic("GetBar",
                                                                new Object[] { }) as List<BarInformation>;
        Assert.IsTrue(true);
    }
}

The first two shim assignments (ShimFoo & ShimQux) are working as expected. But ShimDatabaseFactory.CreateDatabaseString (which is supposed to make DatabaseFactory.CreateDatabase(string) return a stub database when trying to create a new database instance) throws a ShimNotSupportedException again. And I just can't figure out why!
Do you have any ideas what went wrong here?

I would appreciate any input on this.

Thanks,
Benjamin

Was it helpful?

Solution

I had the same exact problem. Try to remove all testsettings files (both from disk and solution), and make sure your solution does not reference any testsettings files.

Also make sure you're using the visual studio testrunner (and not resharper etc. which is instrumenting the code).

I've written two blogposts about these issues which may be helpful:

Visual Studio 2012 Fakes – ShimNotSupportedException when debugging tests

Unit testing – Visual Studio 2012 Fakes in Team City

OTHER TIPS

I have seen this error a number of times with different causes:

  • There is an error or problem in your fakes generation files, some of them are not correct generated. Clean the directories and remake your fake references
  • A depending dll is missing. In this case you are missing a dll on which a fakes dll depends. In one case I was shimming a webservice and missing the System.ServiceModel dll.
  • Sometimes you can fix it with changing your testsetting default processor architecure. However I do not know why, it will probably refresh some cached dlls.

The problem is related to testsettings. I´ve done what the link below suggested and it finally worked after trying lots of possible solutions

http://blog.degree.no/2012/09/visual-studio-2012-fakes-shimnotsupportedexception-when-debugging-tests/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top