Sequencing behaviors

Top  Previous  Next

If you want a fake function to return different values on different calls, Isolator++ Professional makes it easy by allowing you to use the CallTo().WillReturn() combo sequentially. Each subsequent call can return a different value, allowing you to define behavior for a sequence of method invocations.

 

TEST_F(BehaviorSequencingExamples, ReturnTwiceReturnsValuesByOrder)

{

   // Arrange

   auto a = Isolator();  

   auto fakeConcrete = a.Fake.Instance<ConcreteClass>();

 

   Person* personPtr = new Person();

 

   a.CallTo(fakeConcrete->GetString()).WillReturn("hello");  // First call

   a.CallTo(fakeConcrete->GetString()).WillReturn("world");  // Second and more call 

 

   // Act 

   auto result1 = fakeConcrete->GetString();  

   auto result2 = fakeConcrete->GetString();  

   auto result3 = fakeConcrete->GetString();  

 

   // Assert

   ASSERT_EQ("hello", result1);

   ASSERT_EQ("world", result2);

   ASSERT_EQ("world", result3);

}

 

 

Isolator++ Professional employs sticky behavior for these configurations, meaning the last CallTo().WillReturn() call persists throughout the life of the test. The return values are maintained until the Isolator object is deleted. This ensures that you can specify different return values for sequential calls with ease, without needing to redefine behavior for each call. Refer to Test Structure for more information.


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