Recursive Fakes |
Top Previous Next |
Recursive Fakes solve a very common problem with test code - avoiding exceptions thrown because of uninitialized values and pointers returned from faked methods. In order to avoid that, you'd manually need to define several fake objects, and set each method to return a relevant fake object.
This is both cumbersome and error prone. Tests become hard to write and read, and because the immense setup code, it's easy to introduce bugs inside the tests. In addition, inter-dependency between the test and tested code grows, and the test gets more likely to break when you change the tested code. In fact, the changes could have been in another part of the code, and should not have affected the tests altogether.
Over-specified tests, such as these are fragile. With Isolator++ Professional you build robust tests.
Recursive Fakes are the default way that fakes are created with Isolator++ Professional. When you create a fake instance, it is automatically a recursive fake. When you use FAKE_STATICS, all static methods automatically return recursive fakes.
Let's look at an example. We have a Person class:
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 FAKE in our test:
Person* fakePerson = FAKE<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 at writing the setup of the test. The test is now readable, and less error prone. Plus it's now future-proof for changes in the object model.
|
Copyright Typemock Ltd. 2009-2025. All Rights Reserved.