l-september.org> 73ec3a48   
   From: troplin@bluewin.ch   
      
   Öö Tiib wrote:   
   > It has been several times asked in this thread: Why it is difficult to   
   > create fake Singleton::instance() that is instrumented to return an   
   > mock object that you want? Usual answer has been repeated "clear and   
   > obvious" fact that DI is somehow simpler. Ok, but why and how?   
      
   Usually, you test the external interface of a class if it behaves the way   
   it has to. The use of a singleton in the class is an implementation detail   
   and may thus easily be forgotten to test.   
   With DI, you the "singleton" is part of the public interface of the class   
   and can be tested without making assumptions on the internals of the class.   
      
   With DI you have to specify an interface for those "singletons" that may be   
   injected, with a real singleton one is tempted to just create, use and   
   modify it ad hoc. A well defined interface is always good for testing.   
      
   Of course you can replace a singleton with a mock if you really want to.   
   But you cannot just take you library and build a test application around   
   it. You have to exclude the complete singleton implementation from the   
   original code and replace it with a different one.   
      
   In practice that means you have to replace some source files with others   
   just for testing. You cannot use the original binary to build your tests   
   around it, but have to compile (at least link) everything again. Possibly   
   even with different compiler settings.   
      
   I don't say that it's impossiple to test with a singleton mock, but DI is   
   just more convenient.   
      
   Tobi   
      
      
   --   
    [ 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)   
|