我只是试图熟悉Visual Studio 2012 RC中的新Fakes隔离框架,但是我在尝试使用代理时经常遇到ShimNotSupportedException
的问题。
在第一次尝试时,我尝试挂接委托到每个单独的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 在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.InvokeEvent(T value, Action1 eh) 中 在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.OnAttachedUnsupportedMethod(MethodBase method) 中 在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.CheckInstrumentation(MethodBase method) 中 在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.InternalAttachDetour(Object optionalReceiver, MethodBase method, Delegate detourDelegate) 中 在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.AttachDetour(Object optionalReceiver, MethodBase method, Delegate detourDelegate) 中 在Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.SetShimMethod(Delegate optionalStub, Object optionalReceiver, MethodBase method) 中 在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) 中 在System.Fakes.ShimDateTime.set_NowGet(Func'1 value) 中 在GetFoo_ValidBar_ReturnsBaz() 中,位于 BazTests.cs 的第 48 行。
在阅读了我在 MSDN 上找到的两个处理此问题的线程之后,我按照它们的指示 (关闭 CodeCoverage,删除 .testsettings 文件),但并没有起作用! 然而,我已经找到了一个解决办法: 首先从测试资源管理器中运行所有测试(而不是直接使用编程区域中的"MSTest测试(单击运行)"按钮),这样就可以正确地执行所有操作,不会抛出任何异常。随后,我甚至可以调试测试,并且对于 shim 方法的赋值正常工作。 这适用于我使用的所有后续 shims。 但现在,当尝试实现 MS 企业库以进行数据库访问时,我又遇到了同样的问题。
以下是测试的代码:
[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。我就是想不出为什么!
你对这里出了什么问题有什么想法吗?
我会非常感激您的任何意见。
谢谢,
本杰明