我只是想熟悉 Visual Studio 2012 RC 中新的 Fakes 隔离框架,但我因此面临ShimNotSupportedException
s.
在第一次尝试时,我尝试将委托连接到的每个单一 shim 方法ShimNotSupportedException
在尝试运行/调试测试时都抛出了一个。
[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);
}
}
这是相应的堆栈跟踪:
GetFoo_ValidBar_ReturnsBaz 测试方法引发了异常: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 方法) 在 Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.CheckInstrumentation(MethodBase 方法) 在 Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.InternalAttachDetour(Object optionalReceiver, MethodBase方法,委托 detourDelegate) 在 Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.AttachDetour(Object optionalReceiver,Microsoft.QualityTools.Testing.Testing.Fakes.Shims.ShimRuntime.SetShimMethod(Delegate optionalStub, Object optionalReceiver, MethodBase method) 在 Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.SetShim(Delegate optionalStub, Type receiverType) 的 MethodBase 方法,委托 detourDelegate) , 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)在 BazTests.cs 中的 GetFoo_ValidBar_ReturnsBaz() 处的 System.Fakes.ShimDateTime.set_NowGet(Func'1 value):第 48 行。MethodBase 方法)在 Microsoft.QualityTools.Testing.Fakes 的 Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.SetShim(Delegate optionalStub, Type receiverType, Object optionalReceiver, String name, ShimBinding flags, Type returnType, Type[] parameterTypes)在 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:第 48 行。MethodBase 方法)在 Microsoft.QualityTools.Testing.Fakes 的 Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.SetShim(Delegate optionalStub, Type receiverType, Object optionalReceiver, String name, ShimBinding flags, Type returnType, Type[] parameterTypes)在 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:第 48 行。在 BazTests.cs 的 GetFoo_ValidBar_ReturnsBaz() 中的 System.Fakes.ShimDateTime.set_NowGet(Func'1 value) 处键入接收器类型、字符串名称、类型 returnType、Type[] parameterTypes):第 48 行。在 BazTests.cs 的 GetFoo_ValidBar_ReturnsBaz() 中的 System.Fakes.ShimDateTime.set_NowGet(Func'1 value) 处键入接收器类型、字符串名称、类型 returnType、Type[] parameterTypes):第 48 行。
在阅读了我在 MSDN 上找到的处理此问题的两个线程后,我按照他们的说明(关闭 CodeCoverage,删除 .testsettings 文件)对我不起作用!
尽管如此,我还是找到了解决此问题的方法:
首先从测试资源管理器运行所有测试(而不是直接在编码区域之外使用“MSTest 测试(单击以运行)”按钮),一切正常并且没有抛出异常。之后我什至可以调试测试并且分配给 shim 方法的工作与预期一样。
这也适用于我使用的所有以下垫片。
但是现在我在尝试实现 MS Enterprise Library 的伪造品以进行数据库访问时又遇到了同样的问题。
这是测试的样子:
[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);
}
}
前两个 shim 分配(ShimFoo 和 ShimQux)按预期工作。但是 ShimDatabaseFactory.CreateDatabaseString (这应该使 DatabaseFactory.CreateDatabase(string) 在尝试创建新数据库实例时返回一个存根数据库)再次引发 ShimNotSupportedException。我就是不知道为什么!
你知道这里出了什么问题吗?
我将不胜感激对此的任何意见。
谢谢,
本杰明