API Usage

We highly recommend to read the Cookbook in our documentation for more information. 




Faking an Instance

 

Method
Description
MyClass* fakeMyClass = FAKE<MyClass>();
Creates fake instances (can be used of interfaces, abstract and concrete types)
Dependency* dependency= FAKE_ALL<Address>();
Fakes all instances of a type
FAKE_GLOBAL(fopen);
FAKE_GLOBAL(fopen);
 Dependency* dependency= FAKE_ALL<dependency>(FakeOptions::CallOriginal);
Faking methods called in constructor



MyClass* fakeMyClass = FAKE<MyClass>();


ISOLATOR_INVOKE_CONSTRUCTOR(fakeMyClass, 1);


Fakes all methods except the constructor
MyClass* fakeMyClass = FAKE<MyClass>(FakeOption::<CallOriginal>);
Methods will return:
1. For object types: Fake objects
2. For other return types: Zero or equivalent
MyClass* fakeMyClass = FAKE<MyClass>(FakeOption::<CallOriginal>);
Calls to fake object methods will pass through the original implementation. Constructor is called during object's instantiation
FAKE_STATICS<MyClass>();
All public static calls of MyClass are faked and return default values


Changing the Method Behavior


Method
Description
SomeClass* myClass= new SomeClass();
   
WHEN_CALLED(myClass->PublicMethod()).Return(value);
Modifies the behavior of a method:
Fakes the behavior for a live object
SomeClass myClass;
WHEN_CALLED(myClass.Method()).Return(value1);
WHEN_CALLED(myClass.Method()).Return(value2);
...
Fakes a sequenced behavior
SomeClass* myClass = new SomeClass();
WHEN_CALLED(myClass->FirstMethod()->SecondMethod()).Return(10);
Fakes a chain of methods
All arguments are ok WHEN_CALLED(fake->Foo(_)).Return(1);
Conditional behavior faking
All arguments are ok (value type)
WHEN_CALLED(fake->Foo(ANY_VAL(Type))).Return(1);
All arguments are ok (by ref)
WHEN_CALLED(fake->Foo(ANY_REF(Type))).Return(1);
Args must equal (using == op)WHEN_CALLED(fake->Foo(EQ(100))).Return(1);
Args must not equal
WHEN_CALLED(fake->Foo(NE(3))).Return(1);
Arg is smaller than
WHEN_CALLED(fake->Foo(LT(5))).Return(1);
Arg is smaller or equal        WHEN_CALLED(fake->Foo(LE(4))).Return(1);
Arg is greater thanWHEN_CALLED(fake->Foo(GT(10.2))).Return(1);
Arg is greater or equal            WHEN_CALLED(fake->Foo(GE(1))).Return(1);
Arg must pass lambda functionWHEN_CALLED(fake->Foo(IS(<char*>([](char* s) {return  !strcmp(s, "typemock");}))).Return(1);
By ref arg must pass lambda functionWHEN_CALLED(fake->Foo(IS_REF(<cosnt char*>([](const char* s) {return  !strcmp(s, "typemock");}))).Return(1);
Assign out value if condition is trueWHEN_CALLED(fake->Foo(RET_IF(EQ(&value), &out)).Return(1);
Use in condition macros to assign value directlyWHEN_CALLED(fake->Foo(RET_IF(EQ(BY_REF("typemock")), &out)).Return(1);
Custom conditionsWHEN_CALLED(livePerson->IsPartOfGroup(IS(<int>([](int& i) {return  i==2 || i==5 ;}))).Return(true);

SomeClass* myclass= new<SomeClass>();

 

WHEN_CALLED(myClass->PublicMethod())

.DoStaticOrGlobalInstead(OtherClass::OtherMethod, NULL);

DoInstead behavior replacing static or global calls

SomeClass* myClass = new SomeClass();        

AltClass* myAltClass = new AltClass();

   

WHEN_CALLED(myClass->

PublicMethod()).DoMemberFunctionInstead(myAltClass, AltClassMethod);

DoInstead behavior using replacement instance and replacing method


SomeClass* fakeMyClass = FAKE<MyClass>();

WHEN_CALLED(fakeMyClass->PublicMethod()).CallOriginal();

Creating a fully faked instance but making one of functions behave as the real implementation

SomeClass* myClass = new SomeClass();        

WHEN_CALLED(myClass->GetInfoFromMethod()).Throw("Info doesn't exist");

Throw behavior
SomeClass* myClass =  new SomeClass();
 
PCSTR szMessage = "PrivateMethod was called";
PRIVATE_WHEN_CALLED(myClass, PrivateMethod).Throw(szMessage);
Fakes private and protected methods
bool res = false;
ISOLATOR_INVOKE_FUNCTION(res, _, SomeClass::PrivateMethod, 10, 10);
Calling private members on real objects

SomeClass* myClass = new MyClass();
ISOLATOR_SET_VARIABLE(myClass, m_age, 10);
Setting static and instance members (including private and protected members)
int memberValue;
ISOLATOR_GET_VARIABLE(myClass, m_age, memberValue);
Query static and instance members (including private and protected members)


Validating


Method
Description
ASSERT_WAS_CALLED(myClass->MyMethod());
Verifies an instance method call.
ASSERT_WAS_CALLED(SomeClass::MyStaticMethod());
Verifies a static method.
PRIVATE_ASSERT_WAS_CALLED(myClass,MyMethod);
Asserting private methods - an instance method
PRIVATE_ASSERT_WAS_CALLED(_,SomeClass::MyPrivateStaticMethod);
Asserting private methods- a static method
Person* fakePerson = FAKE<Person>();
int num = TIMES_CALLED(fakePerson->GoToWork());
ASSERT_EQ(2, num);
Asserting how many times the method is called
 Person* person = new Person();


 WHEN_CALLED(person->GoToWork()).CallOriginal();


 person->GoToWork();
 int num = TIMES_CALLED(fakePerson->GoToWork());
 ASSERT_EQ(1, num)
Asserting how many times the method is called - live object
int num = PRIVATE_TIMES_CALLED(fakePerson,GoToWork);
Asserting a private method call
int num = PRIVATE_TIMES_CALLED(_,Person::GetAverageAge);
Asserting a static private method call
ASSERT_NOT_CALLED(person->GetAddress());
Asserting a method was not called - instance method
ASSERT_NOT_CALLED(Person::CreatePerson());
Asserting a method was not called - static method
PRIVATE_ASSERT_NOT_CALLED(person, GetAddress);

Asserting a private method was not called
PRIVATE_ASSERT_NOT_CALLED(person, Person::GetAverageAge);

Asserting a private static method was not called 
PRIVATE_ASSERT_NOT_CALLED(person, ChangeLastName, EQ("Smith"));

Asserting a private method with out/by ref value type was not called
ASSERT_WAS_CALLED(livePerson->GetAverageChildAge(EQ(2),LT(2)));

Conditional assertion based on arguments 
ASSERT_WAS_CALLED(livePerson->GetAverageChildAge(IS(<int>([](int& i) {return  i==2 || i==5 ;})),_);

Creating custom conditions