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 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!
0 votes

I have a base class with a public method which is called when a derived class is constructed as follows:

public class Base
{
	public void DoSomething()
	{
	}
}

public class Derived : Base
{
	public Derived()
	{
                ...

		DoSomething();	// Mock this method?
          
                ...
	}
}

Is it possible to mock the base class method so that it isn't called in the derived constructor?

Thanks in advance.

asked by (9.2k points)

2 Answers

+1 vote
 
Best answer

Your in luck, We just added support for base calls in 8.1 

Isolate.WhenCalled(() => fake.BaseMethod()).OnBase().IgnoreCall();

 

Isolate.Verify.OnBase().WasCalledWithExactArguments(() => fake.BaseMethod());

 

Download here

answered by (5.1k points)
selected by
0 votes
I'm not an expert in Isolator's constructor-related mocking abilities so the good folks at Typemock may have a better solution than this one.

By calling a base method from a derived constructor not directly but through a fakable static pass-through method taking a 'this' argument, you can achieve the base method mocking you seek for your derived constructor:
public class Base
{
	public void BaseMethod()
	{
		throw new NotImplementedException();
	}
}

public class Derived : Base
{
	public Derived()
	{
		CallBaseMethod(this);
	}

	public static void CallBaseMethod(Derived derived)
	{
		derived.BaseMethod();
	}
}

[TestFixture, Isolated]
public class DerivedTests
{
	[Test]
	public void Ctor_CallsCallBaseMethod()
	{
		Isolate.WhenCalled(() => Derived.CallBaseMethod(null)).IgnoreCall();
		//
		var derived = new Derived();
		//
		Isolate.Verify.WasCalledWithExactArguments(() => Derived.CallBaseMethod(derived));
	}

	[Test]
	public void CallBaseMethod_CallsBaseMethod()
	{
		var fakeDerived = Isolate.Fake.Instance<Derived>();
		//
		Derived.CallBaseMethod(fakeDerived);
		//
		Isolate.Verify.WasCalledWithExactArguments(() => fakeDerived.BaseMethod());
	}
}

 

answered by (81.6k points)

Thanks for your answer :)

Unfortunately, this requires modifying the existing code which I'm not keen on doing until I have some stable unit tests in place. However, the following seems to be a solution which works for me:

 

    public class Base
    {
        public void BaseMethod()
        {
            throw new NotImplementedException("BaseMethod not implemented!!");
        }
    }

    public class Derived : Base
    {
        public Derived()
        {
            BaseMethod();
        }
    }

    [TestMethod]
    public void TestMethod1()
    {
        Derived fakeDerived = Isolate.Fake.NextInstance<Derived>(Members.CallOriginal, ConstructorWillBe.Called);
        Base fakeBase = (Base)fakeDerived;
        Isolate.WhenCalled(() => fakeBase.BaseMethod()).IgnoreCall();

        Derived derived = new Derived();
    }

 

...