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
Hello All,

Newby to TypeMock Isolator. I'm working on a legacy app at our institution, fixing bugs and what not... :x
App is on MVC.NET, trying to poke as little as possible with the app, and having some issues trying to do some unit tests.

I'm basically trying to fake/Mock a Protected field, I found this on the typemock user guide/online documentation:

Tried following it but I am getting the following error message on the current production code I have.
System.IndexOutOfRangeException : Index was outside the bounds of the array.

For the sake of making things easy to replicate the error, I have reduced the code to the following:

        public void TestingSomething()
            var mockBase = MockManager.Mock<MyBaseControllerClass>();
            var mockedBaseField = mockBase.MockField("dataAccess");
            mockedBaseField.ExpectAndReturn("getMeSomeData", new object());

            var derivedController = new MyDerivedControllerClass();  //<------- This is where the unit test BLOWS UP !!

    public class MyClassWithDataAccessFunctions
        public object getMeSomeData()  { return null; }
    public class MyBaseControllerClass
        protected MyClassWithDataAccessFunctions dataAccess =
            new MyClassWithDataAccessFunctions();
    public class MyDerivedControllerClass : MyBaseControllerClass
        public void FunctionUsingDataAccess()
            var data = dataAccess.getMeSomeData();

So any help would be appreciated, probably I'm doing things incorrectly or in a funky fashion

asked by vazquezed (640 points)

2 Answers

0 votes
Hi edo@rdo.

Welcome aboard :)

Unfortunately the example didn't reproduce the issue.
You used the old API to write this test, I rewrote it with the new API

public void TestingSomething2()
    var baseFake = Isolate.Fake.AllInstances<MyBaseControllerClass>();        //fake the base class
    var DAFake = Isolate.Fake.AllInstances<MyClassWithDataAccessFunctions>(); //fake the data access class
    Isolate.WhenCalled(() => DAFake.getMeSomeData()).WillReturn(new object());//make fake.getMeSomeData() return an Object 
    var objState = new TypeMock.ObjectState(baseFake);                        //acquire the state of the fake base
    objState.SetField("dataAccess", DAFake);                                  //set baseFake to hold DAFake

    var derivedController = new MyDerivedControllerClass();  //<------- Shouldn't blow-up now 

Let me know if it helps.
answered by alex (17k points)
0 votes
Thanks Alex,

Indeed I was using the old API, only through Google I think you can get to the old documentation. While searching other posts I found what you suggest (use new API):

var objState = new TypeMock.ObjectState(baseFake); //acquire the state of the fake base
objState.SetField("dataAccess", DAFake); //set baseFake to hold DAFake

I'm now able to get the UT working, thanks for you time and help

answered by vazquezed (640 points)