Reputation: 1391
I'm currently looking at some unit test librarys in C++ and have some questions:
there seem to be no mocking facility in boost.test but I can hardly think of doing unit tests without creating mock objects/functions. How would you do that in boost.test, are you doing it manually (how? I mean, there are several ways I can think of, none of these seem nice) or are you simply doing without mock objects?
googletest and googlemock looks like nice libraries with mockingsupport however, it requires every object that shall be mocked to be virtual. I don't really like this, it is not that I'm worrying about the performance (I could define a macro to get it out of production code anyway) but I find this very intrusive. I wonder if there's another solution which does not require that much change to the existing code? (love clojure there)
Upvotes: 9
Views: 12183
Reputation: 106530
How else would you expect something to be mockable? That's how it works in every other programming language! (Okay, not with duck typing, but that carries more overhead than virtual methods) If you're concerned about performance:
In all seriousness though, I don't think the virtual calls are going to make huge differences in performance. The one case where virtuals are bad are where they're located inside of inner loops (such as in the iostream
library where they're called possibly for every character of input or output), and even then only in performance sensitive code.
EDIT: I missed the very important word not in the above question #2 -- that you're not worried about performance. If that's the case then my answer is you're effectively screwed. A plain function or method call in C++ generates a plain method call, and there's no opprotunity for you to change where that call points. In most cases this doesn't require too much code change, because correct C++ code uses references wherever possible, which won't need to be modified despite the fact that virtuals are being used. You will have to watch out however for anyone using value semantics, because they will be subject to the slicing problem.
Upvotes: 6
Reputation: 483
Disclaimer I work at Typemock.
Typemock Isolator++ can mock anything!! You don't need virtual - everything is Mockable
See explanation here
So you can fake public, private, abstract (without actually creating a concrete class), non virtual, out arguments, live instance etc... And... It fakes everything recursively
class MyClass
{
int GetResult() { return -1; }
}
We'll use the following code
MyClass* fakeMyClass = FAKE<MyClass>(); // every call to fakeMyClass will be faked
WHEN_CALLED(fakeMyClass->GetResult()).Return(10);
Upvotes: 4
Reputation: 7623
Turtle was designed explicitly for use with Boost.Test and looks very nice to me.
Upvotes: 6