Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Testing Question

Testing Question

Scheduled Pinned Locked Moved Design and Architecture
questiontestingdesignbeta-testingarchitecture
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnalogNerd
    wrote on last edited by
    #1

    I'm fairly new to writing unit tests and I've run into something of a design/architecture question. I'm operating under the belied that any given method should not be overly large, and any that is should be refactored into smaller methods. As a result I end up with this:

    public MyClass : IMyClass
    {
    public string MyMainMethod()
    {
    Method1();
    Method2();
    return "somestring";
    }

    private void Method1()
    {
        // some code
    }
    
    private void Method2()
    {
        // some other code
    }
    

    }

    Obviously this is grossly simplified, but it serves my purposes for this question. I have my classes loosly couples so when writing a unit test I can stub any interface that is injected into MyClass and isolate the code under test. How do I go about stubbing Method1 and Method2? I'm using Moles (and can't change because of company restrictions), but I suspect/hope this is a testing platform independent question. Should I be desiging this different? Not using private methods, but public virtual would allow me more flexibility, but doesn't feel like the right approach. Any advice or pointers would be greatly appreciated. - Andrew

    P B K 3 Replies Last reply
    0
    • A AnalogNerd

      I'm fairly new to writing unit tests and I've run into something of a design/architecture question. I'm operating under the belied that any given method should not be overly large, and any that is should be refactored into smaller methods. As a result I end up with this:

      public MyClass : IMyClass
      {
      public string MyMainMethod()
      {
      Method1();
      Method2();
      return "somestring";
      }

      private void Method1()
      {
          // some code
      }
      
      private void Method2()
      {
          // some other code
      }
      

      }

      Obviously this is grossly simplified, but it serves my purposes for this question. I have my classes loosly couples so when writing a unit test I can stub any interface that is injected into MyClass and isolate the code under test. How do I go about stubbing Method1 and Method2? I'm using Moles (and can't change because of company restrictions), but I suspect/hope this is a testing platform independent question. Should I be desiging this different? Not using private methods, but public virtual would allow me more flexibility, but doesn't feel like the right approach. Any advice or pointers would be greatly appreciated. - Andrew

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Andrew, there are two schools of opinion on this. In one school, you'd effectively elevate the visibility of these methods purely for the purpose of testing. In the second school, you wouldn't directly test these methods. Instead, you'd test the public method that called them. In other words, if there were no route to your private method, then it shouldn't exist.

      I was brought up to respect my elders. I don't respect many people nowadays.
      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      A 1 Reply Last reply
      0
      • P Pete OHanlon

        Andrew, there are two schools of opinion on this. In one school, you'd effectively elevate the visibility of these methods purely for the purpose of testing. In the second school, you wouldn't directly test these methods. Instead, you'd test the public method that called them. In other words, if there were no route to your private method, then it shouldn't exist.

        I was brought up to respect my elders. I don't respect many people nowadays.
        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        A Offline
        A Offline
        AnalogNerd
        wrote on last edited by
        #3

        That makes sense. I guess I was trying to be able to test the private methods individually and separately from the public method that calls them. It sounds like you're saying I could just test them through tests on the public method. This feels cleaner to me, I really didn't like the idea of elevating them just for tests. I made them private for a reason, changing that just for a test felt wrong. Thanks for the feedback, Pete.

        1 Reply Last reply
        0
        • A AnalogNerd

          I'm fairly new to writing unit tests and I've run into something of a design/architecture question. I'm operating under the belied that any given method should not be overly large, and any that is should be refactored into smaller methods. As a result I end up with this:

          public MyClass : IMyClass
          {
          public string MyMainMethod()
          {
          Method1();
          Method2();
          return "somestring";
          }

          private void Method1()
          {
              // some code
          }
          
          private void Method2()
          {
              // some other code
          }
          

          }

          Obviously this is grossly simplified, but it serves my purposes for this question. I have my classes loosly couples so when writing a unit test I can stub any interface that is injected into MyClass and isolate the code under test. How do I go about stubbing Method1 and Method2? I'm using Moles (and can't change because of company restrictions), but I suspect/hope this is a testing platform independent question. Should I be desiging this different? Not using private methods, but public virtual would allow me more flexibility, but doesn't feel like the right approach. Any advice or pointers would be greatly appreciated. - Andrew

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          If there is some need for verifying the code of those functions, I'd use either: - protected instead of private and create a sub-class for testing - reflection for invoking the private methods. But testing is normally about public functions only. And you may have found a situation where that stringency is not fully appropriate.

          1 Reply Last reply
          0
          • A AnalogNerd

            I'm fairly new to writing unit tests and I've run into something of a design/architecture question. I'm operating under the belied that any given method should not be overly large, and any that is should be refactored into smaller methods. As a result I end up with this:

            public MyClass : IMyClass
            {
            public string MyMainMethod()
            {
            Method1();
            Method2();
            return "somestring";
            }

            private void Method1()
            {
                // some code
            }
            
            private void Method2()
            {
                // some other code
            }
            

            }

            Obviously this is grossly simplified, but it serves my purposes for this question. I have my classes loosly couples so when writing a unit test I can stub any interface that is injected into MyClass and isolate the code under test. How do I go about stubbing Method1 and Method2? I'm using Moles (and can't change because of company restrictions), but I suspect/hope this is a testing platform independent question. Should I be desiging this different? Not using private methods, but public virtual would allow me more flexibility, but doesn't feel like the right approach. Any advice or pointers would be greatly appreciated. - Andrew

            K Offline
            K Offline
            Keld Olykke
            wrote on last edited by
            #5

            Hi, If you want to know how much of your private code is tested by your unit tests, there are tools to measure test coverage e.g. NCover. Such a tool may report which code lines are not hit by tests. Usually, each assembly is assigned a minimum coverage that must be reached. A 100% coverage can actually be very difficult to reach. Getting the coverage feedback can be a useful experience. You could get more conscious about how your coding style can make testing easier; branches and exceptions are usually up for discussion. Kind Regards, Keld Ølykke

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups