question

NewCodee-7655 avatar image
0 Votes"
NewCodee-7655 asked karenpayneoregon answered

How to bypass base class method call in MSTest?

I have the following code in c#:

public class MyChildClass : AbstractBaseClass
{
public void myMethod()
{
abstractBaseClassMethodCall();
if(condition)
........
}
}

I have to write a unit test for the above code using MSTest. Here, baseClassMethodCall() fails with error message, "System.NullReferenceException: Object reference not set to an instance of an object."
I cannot change anything in the base class as it is a predefined class with the protected constructor.

Is there a way to bypass the base class method call and continue testing the next line? Maybe by using mocks or shims?




















dotnet-csharpvs-testing
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered NewCodee-7655 commented

Maybe you can use a parameter:

 public void myMethod( bool isTesting = false )
 {
    if( ! isTesting) abstractBaseClassMethodCall();
    . . .
 }

Or divide it, and test myMethod2 separately:

 public void myMethod( )
 {
    abstractBaseClassMethodCall();
    myMethod2( );
 }

 void myMethod2( )
 {
    . . .
 }

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the quick response. But here I can not change anything in the host method/ in the method under test. And the base class method is being used repetitively for some logging purposes. So, I should handle this in my test solution itself.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

A debug session is in order as attempting test is no different from attempting to test something private. Any unit test that uses the abstract class is for testing functionality, not if the method will break somehow.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.