Search code examples
c#.netoopunit-testingencapsulation

How to make class more testable?


I have this:

 class FooGenerator:IFooGenerator {
      private object _generated;

      public void Generate() {
         // Generating
         GenerateSmallPart();
         GenerateOtherSmallPart();
         GenerateTinyPart();
         // A lot similar
      }

      private void GenerateSmallPart() {
         //add small part to _generated
      }

      private void GenerateOtherSmallPart() {
         //add small part to _generated
      }

      private void GenerateTinyPart() {
         //add small part to _generated
      }
   }

   internal interface IFooGenerator {
      void Generate();
   }

In my application I use only IFooGenerator via IoC but I want to test all those sub methods.

As I found here, one option is to extract class with all sub methods. But why do i need to do that. It is only being used in FooGenerator.

Do you have any suggestions how can i make my class more testable?


Solution

  • Who is the client?

    Many people (Roy Osherove, Michael Feathers) consider the test client just as valid as the interface or service client.

    With that in mind I think it's fine to slightly go against the principle of encapsulation by opening up testable seams by making some private methods public.