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,
Is there a way to Mock a class but exempt some members from being mocked?

public class MyClass
{
    [TypeMockExemptThisMember]
    private static DummyClass s_DummyClass = new DummyClass();

    private int i = 0;

    public int GetI()
    {
        return i;
    }
}

public class DummyClass
{
    public void DoSomething()
    {
        //do something;
    }
}


So my goal is to be able to make a fake of MyClass but I want that s_DummyClass to not be mocked.

thanks
asked by pierre-luc (3.3k points)

1 Answer

0 votes
Hi,

Yes you can do that, in the example that you gave you can fake only the instance methods of MyClass and the static field of s_DummyClass will not be faked.

[TestMethod]
public void Test()
{
    // Isolate.Fake.Instance without parameters is like using Members.ReturnRecursiveFakes
    var fake = Isolate.Fake.Instance<MyClass>();
}


Another option is to fake MyClass but use the Members.CallOriginal default
and fake explicitly the methods you are interested in.
[TestMethod]
public void Test()
{
    var fake = Isolate.Fake.Instance<MyClass>(Members.CallOriginal);
    Isolate.WhenCalled(() => fake.GetI()).WillReturn(5);
}


Please let me know if it helps.
answered by ohad (35.4k points)
...