Recursive Fakes |
Top Previous Next |
Recursive Fakes address a common problem in test code—avoiding exceptions due to uninitialized values and pointers returned from faked methods. Without Recursive Fakes, you'd need to manually define multiple fake objects and configure each method to return the appropriate fake object.
This manual setup is cumbersome and error-prone. Tests become harder to write and understand, and the extensive setup code increases the likelihood of introducing bugs. Additionally, tests become tightly coupled with the implementation, making them fragile. Changes in the tested code, even in unrelated areas, can inadvertently break the tests.
Over-specified tests, like the ones described above, are fragile and difficult to maintain. With Isolator++ Professional, you can build more robust, reliable tests.
Recursive Fakes in Isolator++ Professional
By default, Isolator++ Professional creates recursive fakes when you create an instance of a fake object. This is true for all methods using:
When you use a.Fake.Statics, all static methods automatically return recursive fakes.
Let's look at an example. Let’s consider the following Person and Address classes in our application:
class Person { public: Address* GetAddress() { return new Address(); } }
Our Address class looks like this:
class _declspec(dllexport) Address { public: City* GetCity() { return new City(); } std::string GetStreet() { return std::string("5th Avenue"); } }
By calling a.Fake.Instance in our test:
auto a = Isolator(); auto fakePerson = a.Fake.Instance<Person>();
We're actually done.
fakeAddress = fakePerson->GetAddress(); returns a fake Address object. On this fakeAddress object, calling: fakeAddress->GetCity(); returns a fake City object pointer. When a method is called on the City object, it returns a fake object pointer, and so on.
Recursive Fakes not only save you the time writing the test setup - your tests become more readable and less error-prone. They also become future-proof for changes in the object model.
|
Copyright Typemock Ltd. 2009-2025. All Rights Reserved.