 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alex Helder Guest
|
Posted: Mon Jul 11, 2005 7:01 am Post subject: Refactoring class with template methods for unit testing. |
|
|
Hello:
I keep coming across a unit testing situation in C++ for which I have no
answer for. I was hoping somebody could help me out.
Imagine the following scenario
class Thing
{
template <class T>
void do_something (T t){...}
}
void ThisNeedsTesting ()
{
Thing t;
t.do_something(10)
}
Lets say we need to test the function ThisNeedsTesting().
The usual thing to do would be to extract the interface of Thing, and then
derive from this interface the production and test 'mock' objects.
ThisNeedsTesting() would be refactored to ThisNeedsTesting(Interface& i).
Production code would call ThisNeedsTesting() with the real Thing, and
a unit test would pass the mock object.
However, we can't make an interface out of Thing because do_something is a
template function, and they can't be virtual:
class IThing
{
template <class T>
virtual void do_something(T t)=0; <---- oops !
}
class ConcreteThing : public IThing
{
template
void do_something (T t) {...}
}
class MockThing : public IThing
{
...
}
void ThisNeedsTesting (IThing& thing)
{
...
}
I can't figure out how to create sense objects / mock objects out of things
that have template methods. I guess we could use "Template Redefinition"
instead
template <class T>
void ThisNeedsTesting()
{
T t;
t.do_something (10)
}
But this turns most of my program into a header file, because all the
classes / funcs that need to be tested with mocks have to be templates.
Its just a big mess.
Does anybody have any ideas here? It sure would be nice to be able to
declare template member funcs with virtual ...
I'm using the excellent Mockpp library if anybody is curious.
Thanks in advance
-Alex
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
tony_in_da_uk@yahoo.co.uk Guest
|
Posted: Tue Jul 12, 2005 1:32 pm Post subject: Re: Refactoring class with template methods for unit testing |
|
|
Using #ifdef TEST_MODE or similar, you could define
Thing::do_something() differently for test mode, or have
ThisNeedsTesting call Thing::set_test_mode() before "doing something".
It's a template function anyway, so you don't have to worry about
linking with versions from other translation units or libraries that
weren't compiled for test mode.
You could also have some run-time modality in Thing::do_something()
based on environment variables, command line parameters, or whatever
suits.
I'm sure you've thought of these options already - maybe you're just
trying to be too much of a purist. In many ways, C++'s not ready for
that yet.
Cheers, Tony
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|