c# - Unit Testing Methods With File IO -


i'm trying habit of writing unit tests, i've written few before they've been quite basic...i'd start making move tdd want improve quality of code (design , structure) - reducing coupling, while @ same time reduce number of regressions slip through testable build.

i have taken relatively simple project work on begin with. resultant program watches folder , acts on files within folder.

here typical example of code extracted project:

private string restoreextension(string file)     {         var unknownfile = path.getfilename(file);         var ignoredir = path.getdirectoryname(file) + "\\unknown";         string newfile;          //we need library determining mime         if (checklibrary("urlmon.dll"))         {             addlogline("attempting restore file extension");             var mime = getmimetype(bufferfile(file, 256));             var extension = fileextensions.findextension(mime);             if (!string.isnullorempty(extension))             {                 addlogline("found extension: " + extension);                 newfile = file + "." + extension;                 file.move(file, newfile);                 return newfile;             }         }         else         {             addlogline("unable load urlmon.dll");         }          addlogline("unable restore file extension");          if (!directory.exists(ignoredir))         {             directory.createdirectory(ignoredir);         }         var time = datetime.now;         const string format = "dd-mm-yyyy hh-mm-ss";          newfile = ignoredir + "\\" + unknownfile + "-" + time.tostring(format);         file.move(file, newfile);          return string.empty;     } 

questions :

how can test method using io? don't want using real file slow (and more of integration test?), cant see way. add switchable io abstraction layer, sounds me may unnecessarily complicate code...

is project worth unit testing? mean, simple. once strip out .net , 3rd party library calls there not left...so case amount of work make testable means not candidate test?

a lot of methods in project private, project happens windows service. i've read should test methods externally visible (public or exposed through interface), unlikely in case want expose of own methods. project worth testing (as id need either change method access modifiers public or add attributes nunit can see them)?

cheers

regarding how test file i/o: common way of doing encapsulating file i/o in wrapper class. example:

public class filehandler : ifilehandler {      public string getfilename(string name)      {          return system.io.getfilename(name);      }      public string getdirectoryname(string directory)     {          return system.io.getdirectoryname(directory);     } }  public interface ifilehandler {       string getfilename(string name);       string getdirectoryname(string directory); } 

in method under test, program against interface. when run unit test, use mock object returns predefined values. this, use moq.

this bit work not need test file i/o well.

best jan


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -