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
0 votes
I have a class similar to below:

    public class StaticPropertyClass
    {
        protected static string StaticProperty
        {
            get
            {
                return "foo";
            }
        }
    }


How can I mock StaticProperty?

Isolate.Fake.StaticMethods(typeof(StaticPropertyClass));
Isolate.WhenCalled(() => StaticPropertyClass.StaticProperty).WillReturn("bar");


Does not work because there are complaints about StaticProperty not being accessable.

Isolate.NonPublic.WhenCalled(...).WillReturn("bar");


Appears to only work for instance members. How to do this for non-public, static members? I understand that fields are not possible, but how about Methods/Properties?

dmp
asked by dmprantz (640 points)

2 Answers

0 votes
I was reading a bit more in the forum and found some help (Pass in Type) that eventually lead to the answer. This seems to work:

            Isolate.NonPublic.Property.WhenGetCalled(typeof(StaticPropertyClass), "StaticProperty").WillReturn("bar");


Interestingly though, if I use the following, it does not work, only returning the empty string. Does that make sense? What happens if I have to fake public static methods and protected static properties?

            Isolate.Fake.StaticMethods(typeof(StaticPropertyClass));
            Isolate.NonPublic.Property.WhenGetCalled(typeof(StaticPropertyClass), "StaticProperty").WillReturn("bar");


dmp
answered by dmprantz (640 points)
0 votes
Hi,

First off, I'm glad you found the answer in the developer's guide - this is indeed the way to fake static non-public behavior. I will need to look at the issue you report with faking a static property after faking static methods on the type - I'll follow up when I have more info as to why this is happening.

Doron
Typemock Support
answered by doron (17.2k points)
...