Using mocks of overloaded fake functions

2,734 views
Skip to first unread message

Tom

unread,
Apr 12, 2011, 9:36:33 AM4/12/11
to Google C++ Mocking Framework
I am new to GoogleMock as well as using mocks, so pardon me for my
newbieness.

I am building unit tests for an object that uses another object as a
data store. In the data store object, I have a set of overloaded
functions to get data from the data store:
const bool getFeatureProperty(const string& propertyName, string&
propertyValue);
const bool getFeatureProperty(const string& propertyName, bool*
propertyValue);
const bool getFeatureProperty(const string& propertyName, int*
propertyValue);
const bool getFeatureProperty(const string& propertyName, float*
propertyValue);

I set up a fake class that had these methods implemented to facilitate
the testing. When I tried to use Invoke() to call the fake method
inside the mock object, the compiler kept complaining that it could
not figure out which method I wanted to use. Here is the code from
the mock:

class MockFeature : VirtualFeature
{
public:
MOCK_METHOD(getFeatureProperty, const bool(const string&
propertyName, string& propertyValue));

void DelegateToFake()
{
ON_CALL(*this, getFeatureProperty(Matcher<const string&>(),
Matcher<string&>());
.WillByDefault(Invoke(&fake_,
&ConcreteFakeFeature::getFeatureProperty));
}

private:
ConcreteFakeFeature fake_;
};

I tried specifying the parameters in the Invoke() call using
Matcher<type>(), An<type>() and in all of the cases the compiler could
not figure out which method I wanted to use. The "Selecting Between
Overloaded Functions" section of the Cookbook doesn't have an example
with Invoke(). The "Delegating Calls to a Fake" section of the
Cookbook only mentions using a static_cast to specify the function
type, not its parameters.

How do I tell Invoke() which one of the overloaded methods I want to
use?

Thanks,

Tom

Vlad Losev

unread,
Apr 12, 2011, 12:48:03 PM4/12/11
to Tom, Google C++ Mocking Framework
Just cast ConcreteFakeFeature::getFeatureProperty to a particular pointer-to-member type. E.g.:

ON_CALL(*this, getFeatureProperty(Matcher<const string&>(),Matcher<string&>()))
    .WillByDefault(Invoke(&fake_,
        static_cast<const bool(ConcreteFakeFeature::*)(const string&, float*)>(
            &ConcreteFakeFeature::getFeatureProperty)));

you can typedef such a type for easier reading:

typedef const bool (ConcreteFakeFeature::*GetFeaturePropertyFloatMethod)(const string&, float*);
ON_CALL(*this, getFeatureProperty(Matcher<const string&>(),Matcher<string&>()))
    .WillByDefault(Invoke(&fake_,
        static_cast<GetFeaturePropertyFloatMethod>(&ConcreteFakeFeature::getFeatureProperty)));

Thanks,

Tom

HTH,
Vlad

Tom Richenderfer

unread,
Apr 26, 2011, 9:57:04 AM4/26/11
to Vlad Losev, Google C++ Mocking Framework
Vlad,

Tom Richenderfer

unread,
Apr 26, 2011, 10:05:30 AM4/26/11
to Vlad Losev, Google C++ Mocking Framework
Vlad,

Thanks for your help on this. I have changed gears a little in
that I decided to use the "delegate to real" capability instead of
"delegate to fake". I changed my mock object definitions and they all
compiled fine. However, when I specify an instance of my mock object
in a test, I get a large error output with errors like "undefined
reference to g_mock_mutex", "undefined reference to vtable for <my
real class>", and "undefined reference to <methods in my real class>".

I guess my first question is in the example in the cookbook
section for Delegate to Real. The example creates a MockFoo class
that inherits from a Foo class. Is the Foo class the class with all
of the virtual method definitions or is it the concrete class that I
will be referencing in the Invoke() calls? Any conjecture on what I
am doing wrong? I have not included code in this message as I'm not
sure how much I need to divulge vs. corporate secrecy requirements.

Thanks in advance,

Tom

Vlad Losev

unread,
Apr 26, 2011, 1:33:02 PM4/26/11
to Tom Richenderfer, Google C++ Mocking Framework
Tom,

On Tue, Apr 26, 2011 at 7:05 AM, Tom Richenderfer <tric...@gmail.com> wrote:
Vlad,

   Thanks for your help on this.  I have changed gears a little in
that I decided to use the "delegate to real" capability instead of
"delegate to fake".  I changed my mock object definitions and they all
compiled fine.  However, when I specify an instance of my mock object
in a test, I get a large error output with errors like "undefined
reference to g_mock_mutex", "undefined reference to vtable for <my
real class>", and "undefined reference to <methods in my real class>".

   I guess my first question is in the example in the cookbook
section for Delegate to Real.  The example creates a MockFoo class
that inherits from a Foo class.  Is the Foo class the class with all
of the virtual method definitions or is it the concrete class that I
will be referencing in the Invoke() calls?  Any conjecture on what I
am doing wrong?  I have not included code in this message as I'm not
sure how much I need to divulge vs. corporate secrecy requirements.

From the error messages it looks like you are linking your tests neither to the Google Mock library nor to your code under test. In the "Delegate To Real" example in the cookbook, Foo is the real class with virtual methods that the mock class inherits from. In the "Delegate to Fake" example it's an interface.

- Vlad

Tom Richenderfer

unread,
Apr 26, 2011, 3:18:08 PM4/26/11
to Vlad Losev, Google C++ Mocking Framework
Vlad,

Thanks again. I found the typo in my makefile that was not
including the gmock library. That has taken care of most of the
errors except for two:

In function <destructor of my real class>, undefined reference to


vtable for <my real class>

In function MockFeatureSet, undefined reference to <default
constructor of my real class>

Any ideas?

Also, I understand your reply about the delegate to real example
from the cookbook except that the class instance in the private member
section is also class Foo. Does this mean that this is also just the
real class with the virtual methods and not an instance of the
concrete real class that has the real methods?

Thanks again,

Tom

Vlad Losev

unread,
Apr 26, 2011, 10:35:07 PM4/26/11
to Tom Richenderfer, Google C++ Mocking Framework
On Tue, Apr 26, 2011 at 12:18 PM, Tom Richenderfer <tric...@gmail.com> wrote:
Vlad,

   Thanks again.  I found the typo in my makefile that was not
including the gmock library.  That has taken care of most of the
errors except for two:

   In function <destructor of my real class>, undefined reference to
vtable for <my real class>

   In function MockFeatureSet, undefined reference to <default
constructor of my real class>

   Any ideas?

It looks like you are not linking your tests to the library with the code under test. Or it may be that you have forgotten to provide the definition of your real class default constructor somewhere.
Reply all
Reply to author
Forward
0 new messages