chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi! I'm trying to fake calls to AppFabric Caching.

Here's my code:
            var fakeCacheFactory = Isolate.Fake.Instance<DataCacheFactory>();

            //Commented my test logic to isolate the issue.

            Isolate.Verify.WasCalledWithAnyArguments(() => fakeCacheFactory.GetCache(""));


I'm getting this exception:

UseAppFabricCache : Failed*** Failures ***
Message
System.TypeLoadException : Method 'GetCacheProperties' on type 'Mock0000DataCache' from assembly 'DynamicMockAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a0857b23fb3f20d9' is overriding a method that is not visible from that assembly.

Stack Trace
at System.Reflection.Emit.TypeBuilder._TermCreateClass(Int32 handle, Module module)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
at el.a(Type A_0, Object[] A_1)
at f3.a(Object A_0, MethodBase A_1, Object A_2, Boolean A_3)
at e2.a(Object A_0, String A_1, String A_2, MethodBase A_3, Object[] A_4, Object A_5)
at Microsoft.ApplicationServer.Caching.DataCacheFactory.GetCache(String cacheName)
at Aspects.UnitTests.CacheAspectTests.<>c__DisplayClass3.<UseAppFabricCache>b__2() in C:CensoredPathCacheAspectTests.cs:line 74
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Boolean A_5, Object[] A_6)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Boolean isInterceptedType)
at Aspects.UnitTests.CacheAspectTests.UseAppFabricCache() in C:CensoredPathCacheAspectTests.cs:line 0

Halp plz!
Thanks.
asked by Guillaume (3.5k points)

9 Answers

0 votes
Hi,

Isolator creates a dynamic type that extends the interface/abstract class you are faking, and what happened is that the dynamic assembly cannot access the structure of the class it is trying to implement. In order to give the dynamic code access to the internal members, add an InternalsVisibleTo the assembly that needs its types faked:
[assembly: InternalsVisibleTo(MockManager.DynamicMocksAssembly)]

Please let me know if this helps.
answered by doron (17.2k points)
0 votes
Hi,

Another suggestion is to use Isolate.WhenCalled(..).DoInstead({..}) instead Isolate.Verify.WasCalledWithAnyArguments(..).

You can change your code to the following:

var fakeCacheFactory = Isolate.Fake.Instance<DataCacheFactory>();
bool wasCalled = false;
Isolate.WhenCalled(() => fakeCacheFactory.GetCache("")).DoInstead(context =>
{
wasCalled = true;
return null;
});

//Commented my test logic to isolate the issue.

Assert.IsTrue(wasCalled);
_________________
Anna
Typemock Support
answered by anna (260 points)
0 votes
Doron: The type is from microsoft (using Microsoft.ApplicationServer.Caching.DataCacheFactory).

Anna: That totally worked. Thanks!
answered by Guillaume (3.5k points)
0 votes
So Anna's trick worked because I was no longer getting a DataCache. At this point, I need to mock the DataCache itself.

var fakeCache = Isolate.Fake.Instance<DataCache>();


This blows up with the same exception I was originally getting. What now?
answered by Guillaume (3.5k points)
0 votes
Guillaume,

What do you need to do with that DataCache? If it's only few behaviors you need to fake, you can create a real DataCache object and use WhenCalled() on it to fake out specific behaviors.
answered by doron (17.2k points)
0 votes
I'm not sure how I would do that? Here's my code, it bombs with the same exception:

            var fakeCacheFactory = Isolate.Fake.Instance<DataCacheFactory>();
            var fakeCache = Isolate.Fake.Instance<DataCache>(Members.CallOriginal);
            Isolate.WhenCalled(() => fakeCache.Put("", null)).ReturnRecursiveFake();
            bool wasCalled = false;
            Isolate.WhenCalled(() => fakeCacheFactory.GetCache("")).DoInstead(context =>
            {
                wasCalled = true;
                return fakeCache;
            });
            Isolate.Swap.AllInstances<DataCacheFactory>().With(fakeCacheFactory);


Put is one of the methods I'm using and need to fake, but I'm sure if I get this snippet to work I can apply the same code to all the methods I need faked.
answered by Guillaume (3.5k points)
0 votes
Hi Guillaume,

Please contact our support at typemock.com with code sample that reproduces the exception.
We will start a deeper investigation regarding this issue.

_________________
Anna
Typemock Support
answered by anna (260 points)
0 votes
Any update on this one ? I'm trying

var fakeCache = Isolate.Fake.Instance<DataCache>();

and getting:

System.TypeLoadException : Method 'GetCacheProperties' on type 'Mock0000DataCache' from assembly 'DynamicMockAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a0857b23fb3f20d9' is overriding a method that is not visible from that assembly.
answered by dk2 (140 points)
0 votes
Hi,

Thanks, I reproduced the exception you reported about. We are looking for a way to fix it, I'll update you once we'll have a fix.
answered by ohad (35.4k points)
...