Faking concrete classes |
Top Previous Next |
With Isolator++ Professional, you use the same syntax for changing the behavior of any classes, including concrete ones.
Let's say we want to change the behavior of a concrete method:
class MyClass { int GetResult() { return -1; } }
We'll use the following code
MyClass* fakeMyClass = FAKE<MyClass>(); WHEN_CALLED(fakeMyClass->GetResult()).Return(10);
First, we're creating a fake object using the FAKE API. This fakes all the methods on the MyClass class. Primitives return 0, and methods returning pointers, return pointers to fake objects. To further change specific methods, we used the regular WHEN_CALLED API to declare the behavior we want. In this case, we're returning the value 10.
Calling the original constructor
For example if we Fake all the methods of MyClass, but want the constructor to be called.
class MyClass { int m_id;
MyClass(int id) { m_id = id; } }
We'll use the following code:
MyClass* fakeMyClass = FAKE<MyClass>();
ISOLATOR_INVOKE_CONSTRUCTOR(fakeMyClass, 1);
ASSERT_EQ(1, fakeMyClass->m_id))
First, we're fake a MyClass object. Then we're call the constructor using the ISOLATOR_INVOKE_CONSTRUCTOR API with specified constructor arguments, 1 in this case. You can use ISOLATOR_INVOKE_CONSTRUCTOR for public and private constructors.
Note: ISOLATOR_INVOKE_CONSTRUCTOR can be called only once per object's lifetime.
See Faking methods called in constructor for more usages |
Copyright Typemock Ltd. 2009-2023. All Rights Reserved.