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 ShimNotSupportedException
s.
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