Hedayat Vatankhah: Power.Fake.It: PowerFake + FakeIt

As I said in the introduction, PowerFake lacked the features of a complete mocking framework, and I was hoping to be able to integrate it with one or more mocking frameworks. So, I decided to try integrating it with FakeIt as the first target.

Thanks to its flexible design using virtual functions and abstract classes, I was able to integrate PowerFake with it nicely, and the result is PowerFakeIt template class. Using it, you can use almost all of FakeIt tools with free functions and non-virtual member functions, effectively extending FakeIt for such use cases.

You still have to use WRAP_FUNCTION macors to mark the desired functions. But instead of using MakeFake() directly, you can use PowerFakeIt<> class with utilities of FakeIt:

PowerFakeIt<> pfk;

When(Function(pfk, normal_func)).Do([](int ){ cout << "WOW :) " << endl; });

normal_func(100);

Verify(Function(pfk, normal_func).Using(100)).Exactly(1);

PowerFakeIt<SampleClass> pfk2;
When(Method(pfk2, CallThis)).Do([]() { cout << "WOW2" << endl; });
When(OverloadedMethod(pfk2, OverloadedCall, int())).Return(4);
When(ConstOverloadedMethod(pfk2, OverloadedCall, int(int))).Return(5);

SampleClass s;
s.CallThis();
s.OverloadedCall();

Verify(Method(pfk2, CallThis)).Exactly(1);
Using(pfk2).Verify(Method(pfk2, CallThis)
+ OverloadedMethod(pfk2, OverloadedCall, int()));

VerifyNoOtherInvocations(Method(pfk2, CallThis));

 

Going for Virtual Functions

Initially, I didn’t intend to do anything for virtual functions in PowerFake, as I thought it is already covered enough in mocking frameworks. However, soon I realized that it is not true: you also need to use a mock object and pass it to the functions using the virtual functions… but it is not always possible to inject your mock object in the production code, because the function might have used an internal object for its purpose. In some cases, the virtual function call might even got devirtualized by the compiler, which will result in a direct function call rather than an indirect, virtual call. And, PowerFake already supports faking devirtualized calls.

Reading a bit, I figured that I can also cover virtual function calls of GCC in PowerFake. Now that PowerFake can be used with mocking utilities of FakeIt, my next priority is supporting virtual function calls. And, it’ll fake virtual functions of the target class, so every object of that class will call the fake function and there is no need to use & pass  a mock object to the target function.

Finally, note that PowerFake is still in the early stages of development, so no backward compatibility is guaranteed and there might be many corner cases which need to be fixed.

 

 

 

 

 


Source From: fedoraplanet.org.
Original article title: Hedayat Vatankhah: Power.Fake.It: PowerFake + FakeIt.
This full article can be read at: Hedayat Vatankhah: Power.Fake.It: PowerFake + FakeIt.

Advertisement


Random Article You May Like

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*