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 have a static class something like this:
public static class IAmAStaticClass
{
        public static AvailabilityLevel GetInfo(string info)
        {
          ...
         }

        public enum AvailabilityLevel
        {
            NOT_AVAILABLE = 0,
            MESG_FAILED = 1,
            AVAILABLE = 2,
        }

}


I can't figure out how to fake, skip, swap GetInfo(). When I try to do:
            Isolate.Fake.StaticMethods<IAmAStaticClass>();

I get this syntax erroir:
'IAmAStaticClass': static types cannot be used as type arguments


Can anybody think of a way I can get around this issue? I can't make remove the "static" attribute from the class declaration.
asked by (4.2k points)

3 Answers

0 votes
Static classes cannot be used as generic parameters (i.e. they cannot be T in List<T>). You can use the non-generic overload for this, passing the type of the static ciass:

Isolate.Fake.StaticMethods(typeof(IAmAStaticClass));


Alternatively, you can make IAmAStaticClass non-static, but that's up to you.
answered by (5.7k points)
0 votes
igal,
Thanks for your quick response, but I'm having trouble making it work. My code is this:

            Isolate.Fake.StaticMethods(typeof(IAmAStaticClass));
            Isolate.WhenCalled(
                (string info) => IAmAStaticClass.GetInfo(info))
                .AndArgumentsMatch((info) => info == "boo")
                .WillReturn(IAmAStaticClass.AvailabilityLevel.AVAILABLE);


The exception was:

{"The type initializer for 'IAmAStaticClass' threw an exception."}


it is occurred on the WhenCalled line -- is there something elec I need to do to get it to fake it? It wasn't supposed to call it there, I was trying to fake the return value there.
answered by (4.2k points)
0 votes
Ah, I think I know what's going on. In your case, you should use

Isolate.Fake.StaticConstructor(typeof(IAmAStaticClass));


to prevent the cctor from executing. When you use Isolate.WhenCalled, you can specify any method you want, fake or "live", and its behavior will be overwritten, you don't need to explicitly call Fake.StaticMethod().

Also, you can use the Isolator API to verify the argument like this:

string info = "foo";
Isolate.WhenCalled(() => IAmAStaticClass.GetInfo(info))
    .WithExactArguments()
    .WillReturn(...);


By using WithExactArguments you instruct Isolator to check the argument values. They are ignored by default.

Hope that helps.
answered by (5.7k points)
...