c++ - How to mock classes already compiled into a library? -


our code base has collection of components. each component contains "common"/reusable code in form of independent projects built static libraries. examples of components: ui widgets, containers, networking, etc.

when write unit test our ui widgets, building executable links against "ui widgets" static library.

when comes mocking, makes things complicated. normal mocking methods i've read (dependency injection / inversion of control) demonstrated in such way seems difficult (if not impossible) when code being mocked has been compiled.

if ui widgets static library contains implementation of 20 classes, might need mock 5 of those. need somehow tell test executable use 15 symbols static lib ignore 5 (and favor mocked implementations in either library or ideally compiled test executable directly).

how can mock classes in static library effectively? can think of ways using runtime polymorphism + interface pattern, i'd love able mock using templates well. using templates seems more out of reach me here based on structure of projects.

disclaimer work @ typemock.

using templates , polymorphism "mocking" forces adapt production code to tests, instance adding redundant levels of indirection , use of interfaces in places not it, breaking original design.

introduces new classes , interfaces (that require maintenance), complicates code making code larger, less readable , not straightforward you'd be.

typemock isolator++ solves issue enabling mock anything(*abstract classes, non virtual, static, non public, c functions etc...) in runtime, inside tests in library separate production code.

no risk of breaking production code when refactoring more testable.

for example:

class myclass {    int getresult() { return -1; } } 

is faked following way:

myclass* fakemyclass = fake<myclass>(); when_called(fakemyclass->getresult()).return(10); 

see more examples.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -