Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter Faking an out parameter

Faking reference parameters

Top  Previous  Next

In order to fake an out parameter (a value returned from the method, but passed as a parameter), use the RET macro.

 

Let's look at our introductory example. Let's look at our Product class' method IsExpired:

 

 

bool Product::IsExpired()

{

   SYSTEMTIME now;

 

   ::GetSystemTime(&now);

  

   if (now.wYear > 2012)

   return true;

   return false;

}

 

GetSystemTime is a void method, that returns its result through the supplied parameter now. In its regular implementation it returns as a reference the current time. What we want is to fake the method, and return our custom value instead.

Let's look how we use the RET macro inside WhenCalled to do just that:

 

TEST_F(IsolatorPPTests, IsExpired_YearIs2013_ReturnTrue)

{

 Product product;

 

 // Prepare a future time construct

 SYSTEMTIME fakeTime;

 fakeTime.wYear = 2013;

 

 // Fake the GetSystemTime method

 FAKE_GLOBAL(GetSystemTime);

 

 // Replace the returned value when the method is called 

 // with the fake value.

 WHEN_CALLED(GetSystemTime(RET(&fakeTime))).Ignore();

 

 ASSERT_TRUE(product.IsExpired());

}

 

Because it's a global method, we use FAKE_GLOBAL, but it's the same use for a static or an instance call. Following that, we're using WHEN_CALLED to ignore the call to GetSystemTime (and not run the regular implementation), and using the RET macro, supply our custom return value.

When GetSystemTime is called, it just returns fakeTime, and the IsExpired returns true.

 

 

Reference parameters and private methods
 

RET works with private methods as well, for example  

 

 

  PRIVATE_WHEN_CALLED(_, GetSystemTime, RET(&fakeTime))).Ignore();

 

 

In some cases Isolator is not able to figure out the allocation size of an argument, if it's an array, for example. In this case you need to pass a size of an argument in the RET macro:

 

  

  int fakeArr[5] = {1, 2, 3, 4, 5};

  WHEN_CALLED(person->GetSomeArray(RET(&fakeArray, sizeof(int) * ARRAYSIZE(fakeArr)))).Ignore();

 

 

Note: RET will work only for Ignore, Return and ReturnFake behaviors and only when all conditional behaviors are matched.

Note: Use a pointer to the value to RET or use BY_REF

 

 
  WHEN_CALLED(SomeMethod(RET(BY_REF(1))).Ignore();

 


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