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
Is it possible to fake private/internal and thus mock private/internal methods/properties/fields? Example? How about private/internal classes that have non-default constructors (arguments required). How about 'child' classes (where my private classes are).

I use MSTest religously and take advantage of the ability to natively test the private/internal items using the 'accessors' made in the 'shadow' assembly. Often I need to test private classes that are private for a reason; having this ability is essential. How does this framework perform with non-public generics, specifically creating an instance of a custom generic class that isn't public, or a private collection of List<T>? This is often a sore-point.

So far I'm very impressed by the framework - I'm a big fan of AOP, and of PostSharp framework, and this takes things a step further in a direction I like and have been looking for; however, how does this interact with other mocks such as PostSharp? If I happen to be using PostSharp to add an aspect around all my methods for logging purposes or auditing or some other functionality, how is Typemock going to interact? Is one going to destroy the other? If so, can I use Typemock for just regular mocks? Method boundaries are my particular expectations and implementation of interfaces.

What is the feature set difference between the free and pay version? If I use the free version at home playing around, and I say this is great for my company, I need to tell my boss if there are differences.
asked by boo (21.8k points)

3 Answers

0 votes
Hi
I'll try to answer the questions:
1. Mocking private / internal classes:
You can do that once you use InternalVisibleToAttribute.
so you can can call MockManager.Mock method.
Mock mock = MockManager.Mock<MyInternalClass>();

Once you did that you can mock private methods as usual if you are using
reflective mocks.
mock.ExpectAndReturn("MyPrivateMethod", 5);

If you are using natural mocks you can use mocks with MS accessors
see the documentation here

2. Isolator with PostSharp. I don't see any reason why they wouldn't work together. The Isolator is working as a .NET profiler while PostSharp don't. Just to be on the safe side I'll check it and get back to you with an answer.

3. Isolator license. From version 5.0.0 the Isolator does not have the community anymore. With all the licenses below you're getting all the features.
For every new version you get 21 days evaluation after that the options are:
1. Buy Enterprise license.
2. Buy Personal license.
3. Request open source license (for open source projects only)

Hope I answered your questions. :)
answered by ohad (35.4k points)
0 votes
Yes...one more:

How do you mock a static class? static method?

InternalsVisibleTo attribute doesn't allow you to see private members; only internal.

Thanks.
answered by boo (21.8k points)
0 votes
Hi Brian,

Basically you can select any of the following ways to expose internals and privates to the world:

1. Using InternalsVisibleTo (which, as you've already mentioned, exposes ONLY internals. If you have a private type or members it won't be exposed).
2. Use reflection to get internal and private types and members, and then use the results with Isolator API
3. Use MS-Test private accessors. You can then mock them instead.

For testing purposes, I usually go with 1 (which means, turning privates to internals).

As for your other question - Isolator can mock static classes and methods. It can also mock interfaces, abstract and sealed classes.

Let me know if you need additional help,
answered by gilz (14.5k points)
...