matching any const string reference

1,107 views
Skip to first unread message

Steve Fox

unread,
Nov 17, 2010, 7:02:04 PM11/17/10
to Google C++ Mocking Framework
I have a log function which accepts a const string reference as its
second argument. I don't care what the string is, I just want to make
sure log() got called. I cannot use the string matchers because the
function has an overloaded version which accepts a C string and GMock
says that's ambiguous (I assume due to supporting both types of
strings). My test does:

EXPECT_CALL(*tMockDb, log(_, A<const std::string&>()));

but I get the following error.

GMOCK WARNING:
Uninteresting mock function call - returning directly.
Function call: log(6, 0x8061f90 pointing to "subscribers file not
found, creating new file")
Stack trace:
events_test.cpp:55: Failure
Actual function call count doesn't match EXPECT_CALL(*tMockDb, log(_,
A<const std::string&>()))...
Expected: to be called once
Actual: never called - unsatisfied and active

I've tried several variations with Const() and ByRef(), but I can't
figure this one out. Can anyone point me in the right direction?

Zhanyong Wan (λx.x x)

unread,
Nov 18, 2010, 12:29:45 AM11/18/10
to Steve Fox, Google C++ Mocking Framework
On Wed, Nov 17, 2010 at 4:02 PM, Steve Fox <li...@thefoxhome.net> wrote:
> I have a log function which accepts a const string reference as its
> second argument. I don't care what the string is, I just want to make
> sure log() got called. I cannot use the string matchers because the
> function has an overloaded version which accepts a C string and GMock
> says that's ambiguous (I assume due to supporting both types of
> strings). My test does:
>
> EXPECT_CALL(*tMockDb, log(_, A<const std::string&>()));

This says you expect the const string& version of log() to be called.

> but I get the following error.
>
> GMOCK WARNING:
> Uninteresting mock function call - returning directly.
>    Function call: log(6, 0x8061f90 pointing to "subscribers file not
> found, creating new file")

This says that the const char* version is called actually. The
"pointing to" is the give-away: it means the argument is a pointer.

> Stack trace:
> events_test.cpp:55: Failure
> Actual function call count doesn't match EXPECT_CALL(*tMockDb, log(_,
> A<const std::string&>()))...
>         Expected: to be called once
>           Actual: never called - unsatisfied and active

This says that the const string& version is never called, which is the truth.

> I've tried several variations with Const() and ByRef(), but I can't
> figure this one out. Can anyone point me in the right direction?

It's a design smell to have overloaded virtual functions that are hard
to distinguish from the call site. You probably want to make one of
them non-virtual and implement it in terms of the virtual one.

--
Zhanyong

Steve Fox

unread,
Nov 18, 2010, 9:30:31 AM11/18/10
to Zhanyong Wan (λx.x x), Google C++ Mocking Framework
2010/11/17 Zhanyong Wan (λx.x x) <w...@google.com>:

>
> This says that the const char* version is called actually.  The
> "pointing to" is the give-away: it means the argument is a pointer.

That makes sense. I was so wrapped up assuming my matcher was wrong
that I overlooked the obvious.

> It's a design smell to have overloaded virtual functions that are hard
> to distinguish from the call site.  You probably want to make one of
> them non-virtual and implement it in terms of the virtual one.

In the impl class I had the C string version calling the other, but it
makes more sense to do as you suggest.

Thank you much for your help and suggestions.
Steve

Reply all
Reply to author
Forward
0 new messages