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 have an assembly with 2 classes. One of the classes is public with a few methods. These methods make use of the second class which is internal. I am using natural mocking (amazing feature btw :) ) and everything works great until I need to mock the calls to the internal class. The only access to the internal class I have from the test is via the generated code gen accessors (VSCodeGenAccessors.cs). The problem is that the mock for the internal classes ends up mocking the accessor class and not the real internal class.

Here is an example:

public class PublicClass
{
public void publicmethod()
{
int x = InternalClass.internalmethod();
}
}


internal class InternalClass
{
public static int internalmethod()
{
throw new Exception();
}
}

test code:
[TestMethod()]
public void method1Test()
{
PublicClass target = new PublicClass();

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
// The following line obviously doesn't work, but how would you mock an internal class with a static method?
int x = SampleProject_InternalClassAccessor.internalmethod();
recorder.Return(1);
}

target.publicmethod();
}


Any suggestions would be appreciated.

Thanks!
asked by teradyne (600 points)

1 Answer

0 votes
I agree, Natural Mocks are great. 8)

There are two ways.
1. Use Reflective Type Mocks
2. Use [assembly: InternalsVisibleTo("YourTestAssembly")] and you can then just call the internal method.

For information about internal interfaces see This Topic
answered by scott (32k points)
...