Layout of the trouble object

We have the following problem: several classes that we cannot touch, but need a unit test, unfortunately, the classes are not designed for unit testing, so we create mock-ups for checking the code.

Example:

class SomeOtherClass { public: void foo2() { … } }; class ClassToTest { public: ClassToTest() {…} void foo1() { SomeOtherClass A.foo2(); } }; 

In the above example, we would like to test foo1() , but it needs foo2() , so we would like to make foo2() belong to the layout of the object (in real life, these functions / classes are much more complicated and require interaction with hardware configurations, etc. ., thus, the need for mock objects / functions).

So far, we have done something similar, but it really is not optimal, because the code seems to have side effects for other unit tests.

 class MockSomeOtherClass { public: foo2() { … } // mock function }; #define SomeOtherClass MockSomeOtherClass #include "ClassToTest.cpp" ... 

Is there a better way to do this without changing the source classes (or with minimal changes)? We use CPPUnit for testing.

EDIT: added winapi tag for a clearer description of the environment.

+4
source share
1 answer

There is a product called Typemock Isolator ++ that seems to solve the problems you raised. I have not tried it yet, so I can not comment on how well it works or how easy / difficult it is to use.

Unfortunately, you must give them your email address to try. The download is simple enough, but then you are redirected to this page , which fun redirects you to "Register your software now to get a free trial! Enter your details, including a valid email address, to get an activation key to start using Isolator + +. "

+2
source

Source: https://habr.com/ru/post/1384449/


All Articles