Re: [Mockpp-devel] Virtual function
Brought to you by:
ewald-arnold
|
From: Ewald A. <ew...@ew...> - 2006-06-09 18:11:12
|
Hello,
unfortunately I have no perfect solution. You always need some way to switch
between production and test. That's probably what you have to pay for
testability in C++. From my experience you have quite a lot of "unneeded"
virtuals in the end. Everything else tends to become a bit tricky and more
verbose.
What is you actual problem? Performance is usually none and security in the
meaning of preventing to override the method is not safe.
Another solution I can offer is to turn B into a template. Instantiate one for
production using A and one for testing using mock_A.
There might as well be some other way of redirection. For example create a
wrapper template for A/mock_A which helps you to select the correct one:
template <class A_type>
class A_wrapper
{
A_type *a;
A_wrapper (A_type *an_a)
: a(an_a)
{}
void test()
{
a->A_type::test();
}
};
Maybe a compiler switch is acceptable which includes a "virtual" while
compiling debug code:
#ifdef DEBUG
#define DEBUG_VIRTUAL virtual
#else
#define DEBUG_VIRTUAL
#endif
hope that helps a bit :-)
> Hi,
>
> I have the following code :
>
> class A {
> public:
> void test()
> {
> // do some stuff
> }
> }
>
> class A_mock : public A, public MOCKPP_NS::MockObject
> {
> public:
> A_mock() : ex("counter", 0){};
>
> void test(
> ec.inc();
> );
>
> ExpectationCounter ex;
> }
>
> class B
> {
> public :
> A* m_A;
> void f1()
> {
> m_A->test();
> }
> }
>
> int main ()
> {
> A_mock* mock = new A_mock();
> mock->ex.setExpected(1);
>
>
> B varB;
> varB.m_A = mock;
>
> mock->verify();
> }
>
>
>
> Since the test function in the base class A is NOT virtual, polymorphism
> don't work. To work, I have to declarw A::test virtual. My problem,
> is that in my design that function (A::test) should not be a virtual one.
> I find it akward to declare it virtual only for testing. Any hint on
> doing that kind of test with mockpp? I don't want to modify the B class.
>
> Thank you and great work on mockpp
>
> Patrick
--
Ewald Arnold, Germany
mailto:ewald at ewald-arnold dot de
icq:227435282
|