WHEN_CALLED |
Top Previous Next |
To change the behavior and methods, the WHEN_CALLED macro is used.
WHEN_CALLED(myClass->PublicMethod()).<behavior>();
Setting the behavior
After describing the method to fake, choose the behavior by using one of the following APIs:
Depending on the type of function you'd like to change behavior of, you might need to call the following macros first:
In the following example, we use WHEN_CALLED to return a value on a live object.
TEST_F(Examples, UsingWhenCalled) { Person* personPtr = new Person();
WHEN_CALLED(personPtr->GetAge()).Return(34);
ASSERT_EQ(34, personPtr->GetAge());
ISOLATOR_CLEANUP(); }
Changing private methods
PRIVATE_WHEN_CALLED(myClass, PrivateMethod).Return(true);
See Faking private and protected methods
Changing overloaded private methods
PRIVATE_WHEN_CALLED(myClass, PrivateMethod, TYPEOF(bool)).Return(true);
Faking Method Chaining When you have a hierarchy of objects and you want to define a specific behavior for a chain of methods.
// GetPartner method returns A person WHEN_CALLED(person->GetPartner()->GetId()).Return(10);
Note: Every method in the chain will get mocked.
|
Copyright Typemock Ltd. 2009-2025. All Rights Reserved.