dimanche 17 mai 2020

Unit Testing Command Handler which has bunch of responsibilities

Let's assume I have StartCommandHandler which has responsibility to create some file with required files. But for doing this I have to give him a set of sub-responsibilities, like:

  • Checks whether file exists in the FTP
  • If Not downloads files from multiple sources to the temp folder
  • Then executes some script in the folder
  • Then read generated file after script execution
  • Then create zip from that folder
  • Then remove that folder
  • Then updates database

As a result of that Command Handler, we are creating folder with all required files. And now that folder is ready for another operations.

I have just read "Art of the Unit testing". And started to add unit tests. I have followed SOLID principles as well. Especially, SRP and DIP, which are in my opinion prerequisites for Unit Testing. So, most of that things which I stated above are done with specific interfaces. So, 90% job of that Command Handler is to call methods of dependencies. And 10% is the logic like this:

if(!_dependency1.IsAnySomething())
{
     _dependency2.Download();

      var isScriptNeeded = _dependency2.IsScriptNeeded();

      if(isScriptNeeded)
      {
          _dependency3.ExecuteScript();
      }

     _dependency3.Archive();
}

I already tested all dependencies of that command handler. But, hat command handler also includes some small logics like, is download file needed, or temp files are deleted or not and so on...

I have so many question in my mind like:

  1. May be Unit Testing doesn't make sense for such units? Integration Test to the rescue?
  2. Is there any way to refactor that class into more smaller parts? But for now it seems redundant to me?

Aucun commentaire:

Enregistrer un commentaire