Typemock Isolator – Faking an internal static type and overriding a static method

In most common samples about faking static types, the type itself is public as the static methods are too.

Usually programmers tend to expose all members that going to be targeted by an Unit test. Well, that’s not how I see Unit tests.

I always try to produce code to keep the cyclomatic complexity lower, and If I succeeded I ended up with types that can be easily tested.

I won’t expose code simply to test it, code should always have the minimum visibility that it indeed requires.

Then I rely on tools to test all my type members, either private, internal or public.

Currently I use the Typemock Isolator and I must say that I’m completely satisfied.

So, here’s how I faked an internal static type and also override it’s GetString method to allow me test the first parameter value:

Type globalizationHelperType = Type.GetType("NG.Helper, NG", true);
Isolate.Fake.StaticMethods(globalizationHelperType);
Isolate.WhenCalled(() => globalizationHelperType
    .GetMethod("GetString", new Type[] { typeof(String) })
    .Invoke(null, new object[] { null }))
    .DoInstead((callcontext) => { return callcontext.Parameters[0]; });

2 thoughts on “Typemock Isolator – Faking an internal static type and overriding a static method”

  1. Hi Nuno,

    great stuff.
    You can also you the Isolate.NonPublic.WhenCalled method to set the behavior of a specific method, and you can use DoInstead also with it, or you can use Isolate.Verify.NonPublic API (I know, a bit confusing) to verify arguments.

    Gil
    Typemock

  2. Pretty nice post. I have sincerely enjoyed reading your website posts. I have been visiting blogs and sites in similar manner lately and i have to confess you have a nice outline and content. Any way I will be subscribing to your feed and I wish you post more regularly.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>