How to mock a method from outside the class declaration.

543 views
Skip to first unread message

buddyboy

unread,
Jul 21, 2010, 5:30:51 PM7/21/10
to Google C++ Mocking Framework
All of the examples I've found so far show that mocking is done in the
following way

class whatever
{
public:
MOCK_METHOD0(CoolFunction,int(void));
};

But, what if I need to create the mock method in a seperate file
(outside the class declaration). How do I do that?

My code under test includes the real header file for a class whose
methods I want to mock. I don't want to re-declare the class, I just
want to mock a method in it. Is it possible, and if so how?

thanks

Manuel Klimek

unread,
Jul 22, 2010, 5:10:26 AM7/22/10
to buddyboy, Google C++ Mocking Framework

To mock a method, you do not re-declare the class, you inherit from it
and overwrite the method with the mock method. Does that make sense?
If not, can you clarify what exactly your problem is? :-)

Cheers,
/Manuel

buddyboy

unread,
Jul 22, 2010, 10:15:03 AM7/22/10
to Google C++ Mocking Framework
My code under test has to include another class declaration. This
other class is the class I want to mock. I was hoping to be able to
provide the body of the function by simply creating the mock method.
Otherwise I'll have to jump through some hoops to tell the code which
class I want to use. I'll try to illustrate

#include "AlreadyDeclaredClassThatINeed.h"


//This class uses AlreadyDeclaredClassThatINeed
class TestMeBaby
{

};


The class 'AlreadyDeclaredClassThatINeed' has been declared but has no
function bodies yet. Is there a way for me to provide the function
bodies by using mock methods?


On Jul 22, 2:10 am, Manuel Klimek <kli...@google.com> wrote:

Manuel Klimek

unread,
Jul 22, 2010, 10:18:46 AM7/22/10
to buddyboy, Google C++ Mocking Framework
On Thu, Jul 22, 2010 at 4:15 PM, buddyboy <smiling...@gmail.com> wrote:
> My code under test has to include another class declaration. This
> other class is the class I want to mock.  I was hoping to be able to
> provide the body of the function by simply creating the mock method.
> Otherwise I'll have to jump through some hoops to tell the code which
> class I want to use.  I'll try to illustrate
>
> #include "AlreadyDeclaredClassThatINeed.h"
>
>
> //This class uses AlreadyDeclaredClassThatINeed
> class TestMeBaby
> {
>
> };
>
>
> The class 'AlreadyDeclaredClassThatINeed' has been declared but has no
> function bodies yet.  Is there a way for me to provide the function
> bodies by using mock methods?

Yes, this is what gmock is all about. If the methods in
AlreadyDeclaredClassThatINeed are virtual, just follow the guide here:

http://code.google.com/p/googlemock/wiki/ForDummies

Cheers,
/Manuel

>
>
> On Jul 22, 2:10 am, Manuel Klimek <kli...@google.com> wrote:
>> On Wed, Jul 21, 2010 at 11:30 PM, buddyboy <smiling.gyps...@gmail.com> wrote:
>> > All of the examples I've found so far show that mocking is done in the
>> > following way
>>
>> > class whatever
>> > {
>> > public:
>> >  MOCK_METHOD0(CoolFunction,int(void));
>> > };
>>
>> > But, what if I need to create the mock method in a seperate file
>> > (outside the class declaration).  How do I do that?
>>
>> > My code under test includes the real header file for a class whose
>> > methods I want to mock.  I don't want to re-declare the class, I just
>> > want to mock a method in it.  Is it possible, and if so how?
>>
>> To mock a method, you do not re-declare the class, you inherit from it
>> and overwrite the method with the mock method. Does that make sense?
>> If not, can you clarify what exactly your problem is? :-)
>>
>> Cheers,
>> /Manuel
>

--
Manuel Klimek (http://go/klimek)

Reply all
Reply to author
Forward
0 new messages