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
What is the best way to go about testing functionality in abstract classes using TypeMock? I'm looking for a way to test functionality at the abstract level without having to instantiate some concrete subclass of it, but I don't quite see how to use TypeMock to do this.

I think what I'm looking for is the ability to create a whole new mock subclass of the abstraction, and then selectively call real functionality inherited from the abstraction to test it. Can this be done?

Thanks in advance,

Darrell
asked by doverbay (1.1k points)

8 Answers

0 votes
Darrel hi,

Using TypeMock you can Mock any interafce or abstract class without the need to instantiate them. Look at our examples under "Creating a mocked object from an interface/abstract class" for a specific example on how to do that.

Regarding the second part of your question. After you create the MockObject for the interface, you can use it as a regular instantiated object and call various "real" methods from you abstract class:
public abstract class UnderTest
{
    public void Method()
    {
        Console.WriteLine("IN Method");
    }
}

[TestMethod]
[VerifyMocks]
public void testCallORigOnAbstract()
{
  MockObject<UnderTest> mockInterface = MockManager.MockObject<UnderTest>(); 
  mockInterface.ExpectUnmockedCall("Method");

  mockInterface.Object.Method();
}
answered by lior (13.2k points)
0 votes
Darrel,
TypeMock can do exactly as you request, just remember that by default Mocks of Interfaces and Abstract classes are Strict by default (meaning that all calls made to these mocks must be expected).
You can use Lior's trick to call the original code or you can set the mock to not-strict.
public void AllowOriginalCallsOfAbstractClass()
{
  MockObject<AbstractClass> mockedAbstract = MockManager.MockObject<AbstractClass>();
  mockedAbstract.Strict = false;

  mockedAbstract.Object.Method();
}
answered by scott (32k points)
0 votes
Thanks for the replies; this all works great, except I'm still having issues with testing the abstract constructor itself. If the constructor on the abstract class throws an error, how do I test that with typemock?

In my particular case, I don't have a default (parameterless) constructor, and my abstract constructor may throw different exceptions based on the state of the constructor parameters. I want to test that those exceptions are thrown, but when I mock the abstract class, it seems to throw a System.Reflection.TargetInvocationException exception instead of the exception I was expecting to be thrown. Below is an example of what I'm doing; am I going about this the wrong way?

Thanks again for the help,
Darrell

[ExpectedException(typeof(ArgumentNullException), "Argument cannot be null.")]
public void ConstructorTestToVerifyExceptionIsThrown()
{
IAnInterface nullObject = null;
MockObject<AbstractClass> constructorMock = MockManager.MockObject<AbstractClass>(Constructor.NotMocked, nullObject);

Assert.Fail("Exception should have been thrown.", typeof(ArgumentNullException).Name, "Argument cannot be null.");

MockManager.Verify();
}
answered by doverbay (1.1k points)
0 votes
Darrel Hi,

It seems that you have discovcered a defect :oops:
It looks like somwhere inside the exception thrown by the constructor is not passed correctly and is repalced by the exception you get.

We shall investigate this and let you know when the fix will be ready.
answered by lior (13.2k points)
0 votes
Darrel Hi,

It seems that you have discovcered a defect :oops:
It looks like somwhere inside the exception thrown by the constructor is not passed correctly and is repalced by the exception you get.

We shall investigate this and let you know when the fix will be ready.


Based on the above problem, I am also having trouble with testing a protected static method of an abstract class.

Any examples or leads?

Atul
answered by atulbahl (1.2k points)
0 votes
Atul hi,

Mocking static method of abstract class is done the same as any other class.

the following:
[Test]
public void TestStaticOnAbstract()
{
  Mock mock = MockManager.Mock<AbstractClass>();
  mock.ExpectAndReturn("SomeStatic", 10);

  int actual = AbstractClass.SomeStatic();
  Assert.AreEqual(10, actual);
  MockManager.Verify();
}

Mock the call to the static method SomeStatic returning a fake value of 10.

Hope this help
answered by lior (13.2k points)
0 votes
If the methods are just static, they will work but what about "Protected Static" in an abstract class?

Atul
answered by atulbahl (1.2k points)
0 votes
Hi,

Since you are Reflective Mocks the same statement will work for even if the static method is protected internal or private.
answered by lior (13.2k points)
...