Calling A method within an abstract class

+1 vote
I'm trying to fake an abstract class and call one its method and it does not work.

the abstract class has a method called : LoadObjectInSession(string key);

 

I tried the following and each and everytime it returns null ;

 

1)

var con = Isolate.Fake.AllInstances<BaseController>();

Isolate.WhenCalled(() => con.LoadObjectInSession("myKey")).WillReturn("aaa");

2)

MockObject<BaseController> mockInterface = MockManager.MockObject<BaseController>();

mockInterface.ExpectUnmockedCall("LoadObjectInSession");

Isolate.WhenCalled(() => mockInterface.Object.LoadObjectInSession("myKey")).WillReturn("aaa");

 

Please help me understand why everytime it's returning null ?

 

Thank you
asked Nov 5 by aringan (160 points)

1 Answer

–1 vote
Hey aringan,

First of all, the second example you presented here is an old API, so I wouldn't recommend using it.

When I tested the first example on my machine it worked fine.

Which Typemock version are you using?
answered Nov 5 by SapirTypemock (1,130 points)
my version : 8.6.0.10

again neither works for me. when it gets to the following line of code :

string result = LoadObjectInSession("myKey");

although expected value for result is "aaa", however it's null

 

Thank you
Hey aringan,

I'm using version 8.6.0.18 and it works fine for me, so I believe that if you use this version it will work for you too.

Do you have a license key? if so, can you send it to us?

It does not have anything to do with the version. I solved it this way ;

instead of tryni to fake the base class method, I did ;

public abstract BaseController
{
	public object LoadObjectInSession()
	{

	}	
 
}

public class myController : BaseController
{
        var a = LoadObjectInSession();
}

var fakeController = Isolate.Fake.AllInstances<myController>(Members.CallOriginal);

Isolate.WhenCalled(() => fakeController.LoadObjectInSession(FieldNames.UserID)).WithExactArguments().WillReturn(someResult);


 

 

 

 

 

Hey aringan, I'm glad it worked, but I'm still wondering why it didn't work in the first place.

Does the test fail when you run this code?

public class TestClass
{
    [TestMethod]
    public void test()
    {
        var con = Isolate.Fake.AllInstances<BaseController>();

        Isolate.WhenCalled(() => con.LoadObjectInSession("myKey")).WillReturn("aaa");

        object res = con.LoadObjectInSession("myKey");

        Assert.AreEqual("aaa",res);
    }
}

public abstract class BaseController
{
    public object LoadObjectInSession(string key)
    {
        return "Not mocked";
    }
}

 

 

...