Changing Method Behavior


WHEN_CALLED

To specify the method you want to set a behavior on use the macro WHEN_CALLED

#define WHEN_CALLED ( function )

Parameters:

function The function to set the faked behavior on.

Use WHEN_CALLED to set the method that you want to define the faked behavior on. This can be either an instance or static method.

Remarks:

You can then call behavior completing statements (e.g. Return(), Throw() etc.) to define the desired method behavior. Instance behavior change:

fakeConcrete = FAKE(ConcreteClass);
WHEN_CALLED(fakeConcrete->GetString()).Return("hello");

 



Static behavior change: 

FAKE(ClassWithStatics);
/ Creates a faked instance of ConcreteClass.
WHEN_CALLED(ClassWithStatics::GetInt).Return(24);

 

 


See also:


Setting Methods Behavior

The following functions should be used as completing statements to WHEN_CALLED macro. Use them to specify the method behavior.
  1. Return
  2. Throw
  3. ReturnFake
  4. CallOriginal


Return:

 

void Return ( PVOID Value )


Set the return value from faked method.
Parameters:
Value The faked return value.

Exceptions:

Remarks:
Return can be used to fake a return value by value (for primitive types) or by address. Use it as a completing statement to WHEN_CALLED macro.

Example:
/return faked value:
 ConcreteClass* fakeConcrete = FAKE(ConcreteClass);
 WHEN_CALLED(fakeConcrete->GetInt()).Return(100);
 ASSERT_EQ(100, fakeConcrete->GetInt());

 /return faked pointer:
 ConcreteClass* fakeConcrete = FAKE(ConcreteClass);
 ILogger* lp = new ConcreteLogger();
 WHEN_CALLED(fakeConcrete->GetPointer()).Return(lp);
 ASSERT_EQ(lp, fakeConcrete->GetPointer());
 
 


See also:


Throw


void Throw(::std::exception * Excpt ) 
Specify that an intercepted call will throw the given exception.

Parameters:
Excpt The STL exception derived object.

Remarks:
Use Throw method as completing statement to WHEN_CALLED macro. 
Example:
ConcreteClass* fakeConcrete = FAKE(ConcreteClass);
 WHEN_CALLED(fakeConcrete->GetString()).Throw(new
 ::std::exception("Exception occured"));
 
 


See also:


ReturnFake


void ReturnFake ( )

Specify that the faked method should return a faked instance.
Exceptions:
TypemockException Use it when you don't care about the return value behavior. The returned faked instance will have a default recursive behavior.
Remarks:
When using ReturnFake on a void method will cause the intercepted call to be ignored without executing any logic. You can verify that the method has been called by using ASSERT_WAS_CALLED macro. Use ReturnFake method as completing statement to WHEN_CALLED macro. Example:
ConcreteClass* fakeConcrete = FAKE(ConcreteClass);
 WHEN_CALLED(fakeConcrete->GetInt()).ReturnFake();
 ASSERT_EQ(0, fakeConcrete->GetInt());
 
 
See also:


CallOriginal


void CallOriginal ( )

Specify that an intercepted call will try to execute the real logic of the intercepted instance.
Exceptions:
TypemockException

When using CallOriginal on a faked instance the original implementation of the method will be called.

Remarks:
Use CallOriginal method as completing statement to WHEN_CALLED macro. Example:
ConcreteClass* fakeConcrete = FAKE(ConcreteClass);
 WHEN_CALLED(fakeConcrete->GetInt()).CallOriginal();
 
 

See also: