c++ - Best simple way to mock static/global function? -


i have simple value-like class person:

class person { public:     person(thirdpartyclass *object);     virtual ~person(void);      virtual std::string getfullname() const;     virtual int getage() const;     virtual int getnumberofdaystillbirthday() const; }; 

i'm using third party library , thirdpartyclass needs have global/static function called destroy (part of 3rd party library) called on destroy it. destroy function called in person destructor.

now i'm trying unit test person class , need way mock/stub destroy method. think write wrapper class around static destroy function , use dependency injection inject wrapper person class, seems overkill call 1 function on simple class. what's simple straightforward way this? or dependency injection best way this?

update

ultimately decided go creating class wrapped 3rd party library's global functions , using dependency injection pass class constructor of person class. way stub out destroy method. although person class uses single function, other functions of library called @ other points in code , needed test face same issue.

i create single instance of wrapper class in main app code , inject needed. chose go route because think it's clearer. billy oneal's solution , think answers question, realized if leave code few months , come take me longer figure out happening compared dependency injection. i'm reminded of zen of python aphorism "explicit better implicit." , feel dependency injection makes what's happening bit more explicit.

create link seam. put destroy declaration in header, , have 2 implementation files:

// destroy.cpp void destroy() {     //code destruction } 

and testing:

// destroyfake.cpp void destroy() {     //code fake destruction } 

then link first file main binary, , second file testing binary.

beyond this, ask impossible. linker bakes calls global functions , static methods direct jumps callee code -- there's no lookup or decision process hook create type of overload 1 you're looking (beyond having destroy calling that's more mocked).


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -