home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,772 of 33,346   
   Ian Collins to Dave Abrahams   
   Re: Unit Testing Frameworks (was Re: Sin   
   30 Dec 12 14:23:13   
   
   From: ian-news@this.is.invalid   
      
   Dave Abrahams wrote:   
   > on Fri Dec 28 2012, Ian Collins  wrote:   
   >   
   >> Dave Abrahams wrote:   
   >>> on Fri Dec 28 2012, Ian Collins  wrote:   
   >>>   
   >>>> Dave Abrahams wrote:   
   >>>>>   
   >>>>> A logger singleton is probably not the greatest example for   
   >>>>> proving this point, because its actions are usually benign and   
   >>>>> often untested.  However, the use of a singleton may impede or   
   >>>>> prevent the logger from being replaced for testing purposes, or   
   >>>>> prevent different loggers from being used for individual tests.   
   >>>>   
   >>>> How?   
   >>>>   
   >>>> This is the nub of the argument, but nothing posted so far backs   
   >>>> up the assertion.  The preceding posts all demonstrate the use of   
   >>>> different loggers.   
   >>>   
   >>> It seems self-evident to me, but I'll try to explain anyway.   
   >>>   
   >>> * Most singletons are designed not to allow arbitrary lifetime   
   >>>     management at all, so you can only have one during the life of   
   >>>     the executable   
   >>>   
   >>> * In any case, only one instance of a singleton can exist at a   
   >>>     given time, by definition   
   >>>   
   >>> * Therefore if you have two tests that need to talk to different   
   >>>     loggers they either must be in separate executables (in the   
   >>>     first case) or at least can't run in separate threads (in the   
   >>>     second case).   
   >>>   
   >>> What am I missing?   
   >>   
   >> Unit tests normally (in the case of a singleton, nearly always)   
   >   
   > FWIW, I wasn't talking about testing the singleton, but about   
   > testing the components that /use/ the singleton.   
      
   So were we, read the whole sentence.   
      
   >> use mocks for foreign objects required by the tests.  I think that   
   >> was clear from the examples in this thread using a type called   
   >> "MyLogger".   
   >   
   > Even if that's true, I don't see how that relates to my points   
   > above.   
      
   It relates to your points by nullifying them.  As you say, we are   
   talking about testing code that uses a singleton, the foreign object I   
   mention above.  The whole point of using mocks is to break the   
   dependency of the code under test and the real singleton.  The tests   
   will test how the code interacts with the singleton such as:   
      
      does it do the correct thing with data returned by the singleton?   
      
      does it call the correct member functions with the correct   
      parameters?   
      
   The mock object exists purely to support these tests.   
      
   --   
   Ian Collins   
      
      
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca