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 want to mock an internal interface in a signed assembly. Your docs say:
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")]

But this will not work in my case the VStudio compiler will say:
error CS1726: Friend assembly reference 'DynamicMockAssembly' is invalid. Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations.

Any ideas on how to fix this?
Riley
asked by rileytaylor (3.2k points)

5 Answers

0 votes
Riley,
You are correct that there is a problem here. See this Bug in Microsoft
Although you might be able to sign the unit test assembly to solve this. It can be quite confusing as neither assemblies can be built until you get the public key right (See Oliver Strums Blog)

:idea: The easy way out is to use TypeMocks concrete mock ability.
Because the interface is internal, a concrete type that implement the interface is defined in the same assembly.
So a good workaround is to mock the real concrete type (that implements this interface).
Here is a small example:
// production code
internal interface InternalInterface 
{
  ...
}
private class ConcreteClass : InternalInterface
{
  ...
}

The Test
MockObject internalInterfaceMock = MockManager.MockObject(typeof(ConcreteClass));
// make sure the behavior is the same as interfaces
internalInterfaceMock.Strict = true;
InternalInterface theMockedObject = (InternalInterface)internalInterfaceMock.Object
answered by scott (32k points)
0 votes
Thanks Scott, I'll try your idea of the concrete mock.

Your first idea won't solve it, as my unit test assemblies are already signed. I need TypeMock to be signed.

I'm wondering why you don't suggest that TypeMock release some strong-named assemblies? This would solve it.
R.
answered by rileytaylor (3.2k points)
0 votes
TypeMock is signed, it is the Dynamic Library that isn't.
:idea: I will add it to our feature list.
Please tell me if the workaround helped.
answered by scott (32k points)
0 votes
Hi
We created a fix for the problem.
I sent you the fix please check it.
answered by ohad (35.4k points)
0 votes
Hi
The patch is available to all in 4.0.0 release
answered by ohad (35.4k points)
...