a.CallTo

Top  Previous  Next

To change the behavior and methods, use the CallTo API.

 

 

a.CallTo(myClass->PublicMethod()).<behavior>();

 

 

Setting the behavior

 

After describing the method to fake, choose the behavior by using one of the following APIs:

 

API

Meaning

WillReturn()

Return specific value from method instead of calling the real code

WillReturnFake()

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

WillBeIgnored()

Ignore a void function

WillDoInstead()

Call User Supplied Code instead of real code

WillCallOriginal()

Call the real code

WillThrow()

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 APIs first:

 

·Fake.Instance - Use this when you need to fake non-static (instance) methods of a class.
·Fake.Statics - Use this when you need to fake static methods or static member variables of a class.
·Testable.GlobalFunction - Use this when faking global functions, such as C standard library functions (fopen, malloc, etc.).

 

In the following example, we use CallTo to return a value on a live object.

 

TEST_F(Examples, UsingWhenCalled)

{

   // Arrange

   auto a = Isolator();  

 

   Person* personPtr = new Person();

 

   a.CallTo(personPtr->GetAge()).WillReturn(34);

 

   // Act

   auto result = personPtr->GetAge();  

 

   // Assert

   ASSERT_EQ(34, result);

}

 

 

Changing private methods

 
To specify a private method use CallToPrivate:

 

 

a.CallToPrivate(A::Member(myClass, PrivateMethod)).<behavior>();

 

 

See Faking private and protected methods

 

 

Changing overloaded private methods

 
To specify an overloaded method, pass its argument types using A::Type<type>(), or use conditional faking (See Conditional Behavior Faking):

 

 

a.CallToPrivate(A::Member(myClass, PrivateMethod),A::Type<bool>())).<behavior>();

 

 

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.

This is useful 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

 a.CallTo(personPtr->GetPartner()->GetId()).WillReturn(10);

 

 

Note: Every method in the chain will get mocked.

 

Note: To fake Microsoft classes or if your methods return Microsoft types, download the PDB file from the Microsoft Server (See Local Debug Symbols)

 


Copyright  Typemock Ltd. 2009-2025.  All Rights Reserved.