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
The code I am trying to test has this structure:

public class A
{
    public string S
    {
    }
}

public class B
{
    private A _a;

    public B()
    {
        _a = new A();
        _a.S = "Some String"; 
    }
}


Since the instance of B creates an instance of A internally, I need to make sure the internal instance is mocked as well. Therefore, I have written a unit test that looks something like this:

MockManager.MockAll<A>();
var fakeB = Isolate.Fake.Instance<B>(Members.CallOriginal);


This crashes, because I get a NullReferenceException at the point where B tries to set the S property of A. It says that S is null. Why is that? What am I doing wrong?
asked by JeffFerguson (18.2k points)

1 Answer

0 votes
Hi,

The problem here is that you are mixing the old reflective API (MockManager.MockAll) with the new Arrange Act Assert API (Isolate.Fake.Instance).

Here is the correct way to do that:
var fake = Isolate.Fake.Instance<A>();
Isolate.Swap.AllInstances<A>().With(fake);
var fakeB = Isolate.Fake.Instance<B>(Members.CallOriginal);


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