Skip to main content

what iWhat I normally do in c#C# is make my methods protected and not private. itsIt's a slightly less private access modifier, but it hides the method from all classes that do not inherit from the class under test.

public class classUnderTest
{
   //this would normally be private
   protected void methodToTest()
   {
     //some code here
   }
}

now anyAny class that doesn't inherit directly from classUnderTest has no idea that methodToTest even exists. but, what it allows me to do inIn my test code is, I can create a special testing class that extends and provides access to this method...

class TestingClass : classUnderTest
{
   public void methodToTest()
   {
     //this returns the method i would like to test
     return base.methodToTest();
   }
}

thisThis class only exists in my testing project. itsIts sole purpose is to provide access to this single method. itsIt allows me to access inside places that most other classes do not have.

what i normally do in c# is make my methods protected and not private. its a slightly less private access modifier, but it hides the method from all classes that do not inherit from the class under test.

public class classUnderTest
{
   //this would normally be private
   protected void methodToTest()
   {
     //some code here
   }
}

now any class that doesn't inherit directly from classUnderTest has no idea that methodToTest even exists. but, what it allows me to do in my test code is create a special testing class that extends and provides access to this method...

class TestingClass : classUnderTest
{
   public void methodToTest()
   {
     //this returns the method i would like to test
     return base.methodToTest();
   }
}

this class only exists in my testing project. its sole purpose is to provide access to this single method. its allows me access inside places that most other classes do not have.

What I normally do in C# is make my methods protected and not private. It's a slightly less private access modifier, but it hides the method from all classes that do not inherit from the class under test.

public class classUnderTest
{
   //this would normally be private
   protected void methodToTest()
   {
     //some code here
   }
}

Any class that doesn't inherit directly from classUnderTest has no idea that methodToTest even exists. In my test code, I can create a special testing class that extends and provides access to this method...

class TestingClass : classUnderTest
{
   public void methodToTest()
   {
     //this returns the method i would like to test
     return base.methodToTest();
   }
}

This class only exists in my testing project. Its sole purpose is to provide access to this single method. It allows me to access places that most other classes do not have.

Source Link
spaceman
  • 235
  • 1
  • 3

what i normally do in c# is make my methods protected and not private. its a slightly less private access modifier, but it hides the method from all classes that do not inherit from the class under test.

public class classUnderTest
{
   //this would normally be private
   protected void methodToTest()
   {
     //some code here
   }
}

now any class that doesn't inherit directly from classUnderTest has no idea that methodToTest even exists. but, what it allows me to do in my test code is create a special testing class that extends and provides access to this method...

class TestingClass : classUnderTest
{
   public void methodToTest()
   {
     //this returns the method i would like to test
     return base.methodToTest();
   }
}

this class only exists in my testing project. its sole purpose is to provide access to this single method. its allows me access inside places that most other classes do not have.