Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Faking CallBase is not working

0 votes
This is my code:


var mock = MockManager.Mock(typeof(LocationVersionViewModel));
mock.CallBase.ExpectAlways("AfterEditRevertBaseInputComponentVersion");

//creating the fakes
var fakeLocationVersion = Isolate.Fake.Instance<LocationVersionViewModel>(Members.CallOriginal, ConstructorWillBe.Ignored, BaseConstructorWillBe.Ignored);
var newWipfakeLocationVersion = Isolate.Fake.Instance<LocationVersionViewModel>(Members.CallOriginal, ConstructorWillBe.Ignored, BaseConstructorWillBe.Ignored);

//fake static calls
Isolate.Fake.StaticMethods(typeof (LocationVersionViewModel));

//act
fakeLocationVersion.AfterEditRevertBaseInputComponentVersion(null, newWipfakeLocationVersion);

//assert
Isolate.Verify.WasCalledWithAnyArguments(() => LocationVersionViewModel.AddEmptyLocationRowIfNeeded(newWipfakeLocationVersion));


LocationVersionViewModel inherits from BaseInputComponentVersion. BaseInputComponentVersion has a virtual method called AfterEditRevertBaseInputComponentVersion that is overrided by LocationVersionViewModel , and there, I call Base.AfterEditRevertBaseInputComponentVersion .

My goal is to ignore the call to Base.AfterEditRevertBaseInputComponentVersion within LocationVersionViewModel.

Although I have this line: mock.CallBase.ExpectCall("AfterEditRevertBaseInputComponentVersion")
The method AfterEditRevertBaseInputComponentVersion is being call on the base class of LocationVersionViewModel.

Am I doing something wrong?

Thanks,
Busi
asked Dec 12, 2012 by baruchl (120 points)

1 Answer

0 votes
Hi Busi,

I changed the code to simplify the example.

public class BaseInputComponentVersion
{
    public static int field = 0;
    public virtual void AfterEditRevertBaseInputComponentVersion()
    {
        field = 5;
    }
}


public class LocationVersionViewModel : BaseInputComponentVersion
{
    public override void AfterEditRevertBaseInputComponentVersion()
    {   
        //ignore this call
        base.AfterEditRevertBaseInputComponentVersion();
    }
}


The next test achieves your goal:

[TestMethod]
public void TestMethod2()
{
    //Arrange
    var mock = MockManager.Mock<LocationVersionViewModel>();
    mock.CallBase.ExpectCall("AfterEditRevertBaseInputComponentVersion");

    //Act
    var lvm = new LocationVersionViewModel();
    lvm.AfterEditRevertBaseInputComponentVersion();

    //Assert
    Assert.AreEqual( 0 , BaseInputComponentVersion.field);
}


Ignoring the base class's method is possible only with the Mock API.
When
mock.CallBase.ExpectCall("AfterEditRevertBaseInputComponentVersion");
is the line you were looking for.

 

Alex Galin | Support Team

answered Jan 7, 2013 by alex (17,910 points)
...