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,781 of 33,346   
   Richard to All   
   Re: Unit Testing Frameworks (was Re: Sin   
   08 Jan 13 18:20:01   
   
   From: legalize+jeeves@mail.xmission.com   
      
   [Please do not mail me a copy of your followup]   
      
   =?ISO-8859-1?Q?=D6=F6_Tiib?=  spake the secret code   
   <36be4d19-4c67-49e2-9385-43502c25fbef@googlegroups.com> thusly:   
      
   >On Monday, 7 January 2013 21:29:59 UTC+2, Richard  wrote:   
   >> Ian Collins  spake the secret code   
   >> >You appear to have missed the point that you don't use the real   
   >> >singletons in unit testing, you use mocks.   
   >>   
   >> I don't disagree with that approach, but when I have code that is   
   >> directly coupled to Singleton::instance(), how do you slip the mock   
   >> in for testing?   
   >   
   >With a mock Singleton::instance() function that returns mock   
   >Singleton?   
      
   So you're proposing link-time substitution of mock implementations.   
   While this is certainly feasible, it is less than desirable, because   
   it means that noone else in my unit test executable can call the real   
   function, only the mock.   
      
   >> The easiest method is to use DI on the SUT so that it gets an   
   >> instance of the collaborator from it's c'tor instead of through a   
   >> global static method.   
   >   
   >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?   
      
   If the return type of Singleton::instance() isn't an interface, I have   
   to not only supply a link-time substitution for this static method,   
   but also for whatever it returns, which may mean lots of boiler plate   
   that I have to provide by link-time substitution.   
      
   Basically, while link-time substitution is a mocking method that works   
   in C++, it is less desirable than interface polymorphism because it   
   brings along with it extra baggage and constraints.   
   --   
   "The Direct3D Graphics Pipeline" free book    
        The Computer Graphics Museum    
            The Terminals Wiki    
     Legalize Adulthood! (my blog)    
      
      
         [ 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