WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED WHEN_CALLED

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:

 

API

Meaning

Return()

Return specific value from method instead of calling the real code

ReturnFake()

Return a recursive fake object from method instead of calling the real code

Ignore()

Ignore a void function

DoInstead()

Call User Supplied Code instead of real code

CallOriginal()

Call the real code

Throw()

Throw an exception from within the method when method is called

 

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

 
To specify a private method use PRIVATE_WHEN_CALLED

 

 

 PRIVATE_WHEN_CALLED(myClass, PrivateMethod).Return(true);

 

 

See Faking private and protected methods

 

 

Changing overloaded private methods

 
To specify an overloaded method by passing the types of the arguments using TYPEOF(Type) macro or use a conditional faking (See Conditional Behavior Faking).

 

 

 PRIVATE_WHEN_CALLED(myClass, PrivateMethod, TYPEOF(bool)).Return(true);

 

 

See Faking overloaded methods

 

Faking Method Chaining

 
Method chaining is a common syntax for invoking multiple method calls. Each method returns an object that allows the calls to be chained together in a single statement.

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-2021.  All Rights Reserved.