Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class Faking the Turtle Class

Faking the Turtle Class

Top  Previous  Next

We'd like to create a fake instance of the Turtle class. By default, faking an instance of a class, will cause all of that instance's methods to behave like empty methods. If those methods are non-void, they will return either default values for simple values like integers and doubles, and for functions that return other classes, a faked instance will return yet another fake object.

By faking a class, we basically override all of its methods at runtime, so that by default, none of them really do anything.

 

To fake a new instance, we simply use the FAKE macro provided in Isolator.h:

 

     Turtle* t = FAKE<Turtle>();
     std::cout<<t->GetX();

 

 

what we'll get in the printout is '0' because by default GetX() returns the default value, and has no implementation.

 

Note: We don't need to define our own class that inherits from the Turtle class, and how we don't need to define each method's default fake behavior. All of this is done for us freely, at runtime, by using just one line of code.

 

How to use it

Since creating fake objects is so easy with Isolator++, you can just create them directly in a unit test.

Here's an example using GoogleTest:

 

#include "Isolator.h"

 

//setup and teardown support for the test

class MyFirstTestSuite : public ::testing::Test

{

protected:

   virtual void TearDown()

   {

 //clean up all fake declaration after each test

      ISOLATOR_CLEANUP();

   }

};

 

//the test itself

TEST_F(MyFirstTestSuite, FakeATurtle)

{

   Turtle* t = FAKE<Turtle>();

   std::cout << t->GetX();

}

 

//running our tests

int _tmain(int argc, _TCHAR* argv[])

{

   ::testing::InitGoogleTest(&argc, argv);

   return RUN_ALL_TESTS();

}

 

Tearing Down and Cleaning Up

Note in the previous test that at the end of each test, we override the TearDown virtual method of the test framework. we are calling ISOLATOR_CLEANUP - this is very important, since we need to "clean up" the expectations between tests, such as the behavior of static methods, global functions and future objects.

 

//setup and teardown support for the test

class MyFirstTestSuite : public ::testing::Test

{

protected:

   virtual void TearDown()

   {

      //clean up all fake declaration after each test

      ISOLATOR_CLEANUP();

   }

};

 

 

Controlling Fake Behavior

Now we can tell our fake object to return some number:

 

  TEST_F(MyFirstTestSuite, FakeATurtle)

  {

     Turtle* t = FAKE<Turtle>();

     WHEN_CALLED(t->GetX()).Return(42);

 

     int fakeResult = t->GetX();

     ASSERT_EQ(42, fakeResult);

  }

 

 

Here we're using the special WHEN_CALLED macro, which is part of Isolator++.

When using WHEN_CALLED, you specify the method that you'd like to change, inside the parenthesis. You can then decide when the behavior of future invocations that look like this be.

 

The different behaviors you can set for methods:

 

·Return(value): Return custom values from a function (like we did here)
·ReturnFake(): Returns a fake object from functions that return classes (this is the default behavior - so you'll rarely use this)
·CallOriginal(): Makes the invocation call the original implementation of that function, as if it was never fake.
·Ignore(): Makes the invocation do nothing (this is the default for fake void methods)
·Throw(exception|message): you can simulate throwing an exception from a fake method when it is called.
·DoMemberFunctionInstead(instance,function): a callback on an instance of a class to call when the original method is called.
·DoGlobalOrStaticInstead(function): a global or static callback to call when the original method is called.

 

Verifying That Our Turtle Was Called

Now, let's use our fake Turtle to see if its method got called. We're doing to see if our code is working correctly with the Turtle class.

 

TEST_F(MyFirstTestSuite, FakeATurtle)

{

   Turtle* t = FAKE<Turtle>();

   

   ClassUnderTest u = new ClassUnderTest(t);

     

   u.DrawStreightLine(100);

   

   ASSERT_WAS_CALLED(t->Forward());

}

 

Here we used another macro of Isolator++ - ASSERT_WAS_CALLED(invocation).

This allows us to check, after the fact, that any method on our fake object was indeed called by our class under test.

 

If t->Forward() was not called on our fake class, then our test will fail.

It's that easy!


Copyright © Typemock Ltd. 2009-2018.  All Rights Reserved.