How do I create a state based test for a method that calls other methods in the same class?

I have the following code (which I dug into the question):

    public void HandleModeInit(int appMode){
        switch(appMode){
            Case 1:
                DoThis();
            Case 2:
                DoThat();
            Case 3:
                //no mode 3
            Case 4:
                DoSomethingElse();
            Case else:
                //do nothing
        }
    }

      

How would you unit test this method to turn it into an integration test (where do you end up testing DoThis (), DoThat () and DoSomethingElse ())? Since these method calls are made to methods in the same class as HandleModeInit (), how would you test this?

While ideally the method calls would be extracted to a different class, what if this move doesn't make any sense?

+2


source to share


3 answers


If you have control over what is passed to this method, I would pass an interface that takes appmode as an argument, which is AppModeImplementationFactory. The purpose of this factory is to create AppModeImplementations. The factory will look like this:

public class AppModeImplementationFactory: IAppModeFactory
{
   public IAppModeImplementation Create(int appMode)
   {
      // switch case goes here to create the appropriate instance
   }
}

      



When you walk into a factory it might be a mocked instance and you can check if the create method has been called. If you want to test the return type of an instance, you will need to do so under another test. This approach will fulfill your need to execute logic because of the application mode, because the factory will return you an implementation based on appMode.

Hope it helps

0


source


Instead of using a switch / case statement to switch appModes, you can create an IAppMode interface (I'm guessing that the caller can decide which of the objects should initiate, which properly implements IAppMode, since they already pass int represent App Mode).

Your caller will pass the IAppMode object, then your method can call the DoThis () method on the IAppMode.



Then you can create a mock object that implements IAppMode and injects it into the method during testing.

It makes your code more lightweight (using design patterns can do this) and testable.

0


source


I understand (especially if you notice it) that the code is stubbed, but if you don't check what the called methods are doing in this case, what are you actually checking? The value appMode

?

How exactly to attack it is difficult (if not impossible) to say without knowing what is happening inside the called methods. Are they making any outgoing calls (external services, database, etc.) that can be mocked? If so, this may be the way forward. If they only contain behavior encapsulated in the class, maybe they change some property value that you can check when the call returns HandleModeInit

?

I am generally not a fan of redesigning my code with the sole purpose of making it more testable, but one approach would be to separate your class's public interface from the internals, and then define the backend as, well, an interface. Thus, you open up the insides to bullying.

0


source







All Articles