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,550 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";
    }
}

 

 

I ran your test and it passed. 

I also tried the following and it worked ;

public void SomeMethod()
{
            var fakeBaseController = Isolate.Fake.AllInstances<BaseController>(Members.CallOriginal);

            Isolate.WhenCalled(() => fakeBaseController.LoadObjectInSession("myKey")).WithExactArguments().WillReturn("aaa");
}

I assume the problem was I missing  ( Member.CallOriginal  ).

 

I appreciate your respond and help 

 

Hey aringan,

That's not a problem at all, I'm happy to help!

Members.CallOriginal sets the default behavior of the fake object, so I don't think it has something to do with it.

Notice that when you use WhenCalled like this:

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

It doesn't mean that when you call con.LoadObjectInSession() with "myKey" it will return "aaa", it means that when you call con.LoadObjectInSession() with any argument it will return "aaa".

For example, this test will pass as well:

public void test()
{
      var con = Isolate.Fake.AllInstances<BaseController>();

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

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

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

If you use WhenCalled like this:

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

It does mean that only when you call con.LoadObjectInSession() with "myKey" it will return "aaa".

Maybe that misunderstanding is what caused the problem..

...