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
How to change behavior of a non-public method with an [out] parameter?
asked by Noob (2.1k points)

1 Answer

0 votes
Isolator still doesn't have a specific API for doing this. So, in order to change the behavior of a non-public method with an out parameter, we will use the DoInstead method of Isolator.

DoInstead lets us write an alternative implementation for the faked method.
Inside the DoInstead block, we will use the context object to get access to the parameters passed to the method.

These parameters are indexed in ctx.parameters[] array, arranged by their order in the method's signature (LTR).

In this example, we fake the private func GetBools.
To get the out bool[] bools argument inside the DoInstead block, we use ctx.Parameters[0].

public class Boo
        private bool[] boolsDM;

        private void GetBools(out bool[] bools)
            bools = new bool[10];

public void TestOutParams()
      Boo b = new Boo();
      Isolate.NonPublic.WhenCalled(b, "GetBools").DoInstead(ctx => 
          // Here we replace the out parameter to a controlled array
          // When the method gets called, it will be faked and will return the controlled array
          ctx.Parameters[0] = new bool[100];  

      bool[] testArray = b.GetBoolsDM();

      Assert.AreEqual(100, testArray.Length);
answered by NofarC (4k points)