More than one test for a single method. RRS feed

  • Question

  • Hi,

    I have a method which checks for multiple business logics. I need to write a test method for each business logics. Is it possible?

    basically the method does a sequece of actions in between each achtion we check some business logic. For example if i have n actions and n business logics in the method. To create the test method for n'th logic i need to pass through all previous logics.

    Please help me on this.


    Friday, November 19, 2010 7:35 AM

All replies

  • Hi Muthusam,

    In general you should not combine logic in methods. Keep them lean and mean; i.e. use Dependency injection to separate the logic. I'm assuming you are going to use Unity from the Entlib framework (use 4.1 or 5.0)

    What this means to your case is that:

    1. you need to split up your logic into one or more separate helper classes
    2. create interfaces on each of them
    3. refactor you existing code so that it makes calls to the helper classes (you now end up with a controller class / method that invokes methods on other classes)
    4. Instantiate the helper classes in either the constructor of the class or use [Dependency] attributes (look this up in the Entlib documentation)
    5. Use stubs in your test method to stub out any of the calls you do not want to test.
    For stubs I would recommend Rhino Mocks or Moq. Have a look at their documentation how this is performed. All in all, this is not trivial to do, but you will benefit greatly from the learning expierence / better design. It should, however take about a day to master starting from point 0 (assuming you are not familiar with any of these techniques).

    Kind regards,
    Tom de Koning

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Tom de Koning Friday, November 19, 2010 9:00 AM
    Friday, November 19, 2010 8:59 AM