ShimNotSupportedException在MS的VisualStudio 2012 [英] ShimNotSupportedException in MS VisualStudio 2012

查看:335
本文介绍了ShimNotSupportedException在MS的VisualStudio 2012的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是试图让在Visual Studio 2012 RC熟悉新的正版正货分离框架,但我因此与 ShimNotSupportedException 取值面临的问题。
结果在第一次尝试,我试图挂钩委托每个单独的垫片的方法来,只好抛出试图运行/调试测试时, ShimNotSupportedException



<预类=郎-CS prettyprint-覆盖> [TestMethod的]
公共无效GetFoo_ValidBar_ReturnsBaz()
{
按(ShimsContext.Create())
{
ShimDateTime.NowGet =()=>新的DateTime(2012,08,11,10,20,59);

常量字符串预期=20120811_102059;
串实际=的getFoo();

Assert.AreEqual(预期,实际值);
}
}

这是对应的堆栈跟踪:





GetFoo_ValidBar_ReturnsBaz
测试方法已抛出异常:
Microsoft.QualityTools.Testing.Fakes.Shims.ShimNotSupportedException :
System.DateTime的
在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.InvokeEvent(吨价,措施1 EH)在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime
。 OnAttachedUnsupportedMethod(MethodBase法)
在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.CheckInstrumentation(MethodBase
法)
在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.InternalAttachDetour(目标
optionalReceiver,MethodBase方法,代表detourDelegate)在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.AttachDetour
(对象
optionalReceiver,MethodBase方法,委托detourDelegate)
。在Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.SetShimMethod(代表
optionalStub,对象optionalReceiver,MethodBase法)
在Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.SetShim(代表
optionalStub,类型receiverType,对象optionalReceiver,字符串名称,
ShimBinding标志,类型的返回类型,类型[] parameterTypes)在Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.SetShimPublicStatic(代表$ b $
b optionalStub,类型receiverType,字符串名称,类型的返回类型,类型[]
parameterTypes)
在System.Fakes.ShimDateTime.set_NowGet(Func'1值)
在GetFoo_ValidBar_ReturnsBaz()
在BazTests.cs:48行




看了两个线程我在MSDN已经找到处理这个问题我也跟着他们的后说明(车削代码覆盖率关闭,删除.testsettings文件),它并没有对我的工作!
结果不过我发现这个问题的解决方法:
结果通过首先运行在测试资源管理器的所有测试(而不是使用MSTest的测试(点击运行)按钮,直接退出编码区)一切运行正常,没有异常被抛出。后来我甚至可以调试试验和分配给垫片的方法如预期只是工作。
结果这个工作对所有后续垫片我用为好。
结果但现在我想实现数据库访问的MS企业库的假货时再有同样的问题。



这是什么测试如下:



<预类=郎-CS prettyprint-覆盖> [TestMethod的]
公共无效GetFooFromEF_NonEmptyDataReader_ObjectsCorrectlyInstantiated()$ b $使用(ShimsContext.Create()){
变种的DataReader =新StubIDataReader()
{
ItemGetString = S = GT b {
; 1,
DepthGet =()=> 2
};

ShimFoo.GetBar = GUID => DataReader的;

无功吧=新StubIBar()
{
ConvertIBarToBaz =纪录=>空
};

ShimQux.AllInstances.GetBar =(A,B)=>酒吧;

变种dbFactory =新StubDbProviderFactory();
变种DB =新StubDatabase(测试,dbFactory);
ShimDatabaseFactory.CreateDatabaseString = S = GT;分贝;

名单,LT; BarInformation>实际= accessor.InvokeStatic(GetBar,
新的对象[] {})的名单,LT; BarInformation取代;
Assert.IsTrue(真);
}
}

前两个垫片分配(ShimFoo&安培; ShimQux)是否按预期工作。但ShimDatabaseFactory.CreateDatabaseString(这是应该做DatabaseFactory.CreateDatabase(字符串)试图创建一个新的数据库实例时返回存根数据库)再次抛出ShimNotSupportedException。我只是想不通,为什么!
结果你有什么想法出了什么错在这里?



我希望有这方面的投入。



谢谢,
结果本杰明


解决方案

我有同样的确切问题。尝试(包括从磁盘和解决方案)删除所有testsettings文件,并确保您的解决方案不引用任何testsettings文件。



另外,还要确保你使用的可视化。工作室的TestRunner(而不是等ReSharper的是插装代码)



我写这些问题可能​​会有所帮助2的相关博客文章:



的Visual Studio 2012正版正货 -



调试测试时ShimNotSupportedException

单元测试 - 的Visual Studio 2012中假货的团队市


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

解决方案

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

这篇关于ShimNotSupportedException在MS的VisualStudio 2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆