升级 Visual Studio 2013 和 Microsoft Fakes v12 后出现 ShimNotImplementedException

ShimNotImplementedException after upgrading Visual Studio 2013 and Microsoft Fakes v12

我们在 Visual Studio 2013 中使用 Microsoft Fakes。更新到 Visual Studio 2013 Update-4 或 Update-5 后,我们在测试中得到 ShimNotImplementedException

我们有 followed instructions found in other SOF questions 并关闭了 Microsoft.QualityTools.Testing.Fakes 引用的 SpecificVersion。这允许编译,但在 运行.

时测试仍然失败

我们在 MSDN forums 中找到了解决此问题所需的提示。

The underlying issue is that the legacy tests did not define specific methods on the ShimXXX object that the code based is using. Under version 11 all is well; version 12 is a different matter.

ShimNotImplementedException 的堆栈跟踪提供了有关缺失 property/method 的所需信息:

Microsoft.QualityTools.Testing.Fakes.Shims.ShimNotImplementedException
at $Func`2NotImplementedf5b9281e-32b0-4bf3-9079-6a54470670de.Invoke(SiteContext arg1)
at Sitecore.Sites.SiteContext.get_Database() //THIS IS THE PROBLEM PROPERTY
at Sitecore.Ecommerce.ShopContext..ctor(SiteContext innerSite)
at ActiveCommerce.UnitTest.ProductStockManagerTests.get_MockShopContext()
at ActiveCommerce.UnitTest.ProductStockManagerTests.GetAvailability_AlwaysInStock()

将缺失的 属性 添加到我们的垫片构造中解决了问题:

        return new Sitecore.Ecommerce.ShopContext(new ShimSiteContext
        {
            PropertiesGet = () => new NameValueCollection(),
            DatabaseGet = () => null //ADDING THIS SOLVED THE ISSUE
        });

我 运行 在使用 Visual Studio 2015 将我们的几个项目从 .NET 4 升级到 .NET 4.5.2 后遇到了类似的问题。突然之间,之前通过的几个测试开始失败。共同点是所有测试都使用 Shims 来模拟注册表访问。

似乎发生的事情是处理 Dispose 方法时发生了一些变化。最初我没有在 RegistryKey 垫片上实现 Dispose 方法。在 .NET 4 下,这似乎没有造成任何麻烦 运行。但是在切换到 4.5.2 之后,它一直被隐式调用。

解决方案很简单:我只是为 Dispose 添加了一个存根。

Microsoft.Win32.Fakes.ShimRegistryKey.AllInstances.Dispose = (key) => { };

测试现在再次通过。

请注意,将其设置为 NULL 不会 解决它。必须有一个方法。