Return

Top  Previous  Next

To return values from faked methods, simply use WHEN_CALLED and then Return.

The following table explains possible behaviors:

 

Faked Method Deceleration

Return syntax

Description

int Foo();

WHEN_CALLED(...).Return(10);

When the return value is an Integer, just return an integer.

 

string Foo();

string bus("Bus");

WHEN_CALLED(...).Return(&bus);

 

When the return value is a String, just return a string by reference, or an instance string.

 

Bar *Foo();

WHEN_CALLED(...).Return(null);

WHEN_CALLED(...).Return(&bar);

When the return value is a Pointer, you can return an object by reference (for value objects), a pointer to an object, or null.

 

char *Foo();

WHEN_CALLED(...).Return("dummy");

 

When the return value is "Char *", you can also (in addition to the pointer section) return an instance string.

vector<Bar> Foo();

 

WHEN_CALLED(...).Return(BY_VAL(testVector));

 

When the return value is an Object-By-Val, use the BY_VAL macro to return the object.

 

Examples

 

 

TEST_F(Examples, ChangeReturnValue)

{

   Person* fakePerson = FAKE<Person>();

   

   WHEN_CALLED(fakePerson->GetAge()).Return(10);

   

   ASSERT_EQ(10, fakePerson->GetAge());

 

   ISOLATOR_CLEANUP();

}

 

 

To return objects that are not values, return a reference or pointer to that instance

 

TEST_F(Examples, ChangeStingReturnValue)

{

   Person* fakePerson = FAKE<Person>();

   string bus("Bus");

 

   WHEN_CALLED(fakePerson->GetTransportName()).Return(&bus);

   

   // continue will test...

 

   ISOLATOR_CLEANUP();

}

 

 

To return objects by value, use BY_VAL(object) macro in Return:

 

TEST_F(Examples, ReturnLocations)

{

   GPSLocation testLocation = GPSLocation (1, 1);

   Person realPerson = Person();

 

   vector<GPSLocation> testStopsVector;

   testStopsVector.push_back(GPSLocation(0, 0));

   testStopsVector.push_back(testLocation);

   testStopsVector.push_back(GPSLocation(2, 2));

 

   ITransport* fakeTransport = FAKE<ITransport>();

   WHEN_CALLED(fakeTransport->GetAllStops()).Return(BY_VAL(testStopsVector));

 

   realPerson.SetTransport(fakeTransport);

 

   ASSERT_TRUE(realPerson.TransportStopsAt(testLocation));

 

   ISOLATOR_CLEANUP();

}

 

Note: In order to return by value, Typemock utilizes copy, default constructors or assignment operator to replicate values.
Occasionally, due to linker issues, these methods may not be incorporated into the executable.
If this happens, call those constructors/operators somewhere in your code to achieve the proper functionality.

For Example:

if (false) 

{
     auto dummy1 = ReturnType(); //Calling the Default Constructor

     auto dummy2 = ReturnType(dummy1); //Calling the Copy Constructor

}

In certain situations, it may be necessary to download a PDB file from the Microsoft Server in order to retrieve the required information.

 

 

To return Enum, simoply use the Return()

 

TEST_METHOD(Examples, ReturnEnum)

{

   Person * fakePerson = Fake<Person>();

 

   WHEN_CALLED(fakePerson->GetHairColor()).Return(HairColor::Blonde);

   int result = fakePerson->GetHairColor();

 

   Assert::AreEqual(static_cast<int>(HairColor::Blonde),result);

}

 

 


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