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
i tried to mock an internal abstract class but got an exception with "Access is denied" message :( . Any ideas on how to fix this? Thanks.
asked by patrotter (800 points)

8 Answers

0 votes
Hi and welcome to the forum.
Sure you can do this.
Have a look here

From TypeMock.NET documentation:
In .NET 2.0 it is possible to mock the internal interfaces if you give TypeMock the correct permissions, as follows:
In the AssemblyInfo of the internal interface add
[assembly: InternalsVisibleTo("DynamicMockAssembly")]
answered by ohad (35.4k points)
0 votes
I had tried that but because the assembly has a strong name you need a key for that (i.e. [assembly: InternalsVisibleTo("DynamicMockAssembly, <key number>")])
answered by patrotter (800 points)
0 votes
Ohad,

Thank you for your reply.

Some more information on trying this. When I tried to compile with the line added to assembler info I got the following compilation error.

"Friend assembly reference 'DynamicMockAssembly' is invalid. Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations."

I tried getting the strong name for "DynamicMockAssembly" but could not find the file anywhere on my system.

Thanks again.
answered by patrotter (800 points)
0 votes
Ohad,

I did try it for the type mock assembly

i.e.
"[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("TypeMock, PublicKey=0024000...)]"

the code compiled but the test was unable to mock the internal abstract class.

Thanks.
answered by patrotter (800 points)
0 votes
Ohad,

The reason that DynamicMockAssembly is denied access is because it is not given a strong name when create by the TypeMock assembly. This means any assembly with a strong name will not give visible internal priviliges to DynamicMockAssembly.

The work around that I have done is to give my test assembly visible internal priviliges to the assembly I want to test. In my test assembly I create a concrete class derived from my internal abstract class and mock that.

That seems to work.

Regards,

Peter Trotter.
answered by patrotter (800 points)
0 votes
Hi
I found out that our documentation is not complete in this issue. :(
Thanks for pointing the problem, We will fix it.

Here are the steps you should take to make it work:
Note that the order is significant.

Sign your test assembly
Build the test assembly.
Extract the public key of the test assembly like this:
sn.ex -Tp MySignedTestAssembly.dll
This will write the public key to the command line output.
Add this lines to the assembly you want to test:
[assembly: InternalsVisibleTo("DynamicMockAssembly=0024..")] //DynamicMockAssembly key
[assembly: InternalsVisibleTo("MySignedTestAssembly, PublicKey=1234..")]  //Your test assembly key

The PublicKey of MySignedTestAssembly should be the extracted key from sn.exe output.
Build again.

After all these steps you should be able to mock internal abstract classes
and interfaces without creating the concrete implementation.
answered by ohad (35.4k points)
0 votes
Hi,

Thanks for looking into this. Just one question - how do I get the key for the dynamic mock assembly. I tried using the key for my testing assembly but that did not seem to work.

Thank you for your help.
answered by patrotter (800 points)
0 votes
Hi
You can get the key for DynamicMockAssembly in the example at the help

[assembly: InternalsVisibleTo("DynamicMockAssembly,PublicKey=0024000004800000940000000602000000
240000525341310004000001000100ab8e3015b99a732d20ecb2a29fb3f54288a8a614896e7c5091d7b9045368fe6b8
bfcc72dce4f01b71281eb4e380dcb709c83a5042a54c684a4711248c078fefb01bcdb09a6ce252e0304ed08c6e4ddf6
9212e3d0a770d953572e3c474fc08fe3bdbb2fad97b32c6045c08f34466dc8e07bd255d3dbc72408dce6859edb4b04bf")]


:arrow: You can use instead:
[assembly: InternalsVisibleTo(MockManager.DynamicMocksAssembly)] 

But than you'll have to reference TypeMock from your tested assembly
(Not nice) :?
answered by ohad (35.4k points)
...