Using Eq to compare non-primitive method arguments

1,525 views
Skip to first unread message

Steffen

unread,
Sep 27, 2011, 6:49:39 AM9/27/11
to Google C++ Mocking Framework
I am checking if a method calls a mocked object method with the right
argument.

Therefore in my test im calling encodeCharatcerStringValue(const
String & value, Writer& writer) and expect that it calls
writer.write(const String& value) (the value does not get changed in
the encode method, its a pretty basic test).

///////////////////////////////////////////////////////////////////////////////
void testEncodeCharacterStringValueNonEmptyValue() {
String value = "abc";

System::Registry().replaceProperty("plugins.writer.text.file.outputbuffersize",
"1");

// we want to see the writer.write(value) method called exactly
once, with value "abc"
ASN::Plugins::Writer::Text::Stream::MockedWriter writer;
EXPECT_CALL(writer, write(Eq(String("abc")))).Times(1);
m_encodingControl->encodeCharacterStringValue(value, writer);
}

The writer and its write method are mocked.

The above test works, if I change the expected result to "abd" it
fails, so everything works as expected. What I am wondering about is
how does google mock compare the two objects? Both are of type String,
which is a class inside our framework that wraps certain string
implementations.

I would like google mock to use the operator== or compareTo
implementation of the String class for the matching, but debugging
tells me none of those methods get called.

So how does google mock compare the two objects via Eq? I'm worried
simply comparing the whole objects may fail the test under some
cirumstances.

Zhanyong Wan (λx.x x)

unread,
Sep 27, 2011, 2:41:54 PM9/27/11
to Steffen, Google C++ Mocking Framework
On Tue, Sep 27, 2011 at 3:49 AM, Steffen <ste...@newgods.org> wrote:
> I am checking if a method calls a mocked object method with the right
> argument.
>
> Therefore in my test im calling encodeCharatcerStringValue(const
> String & value, Writer& writer) and expect that it calls
> writer.write(const String& value) (the value does not get changed in
> the encode method, its a pretty basic test).
>
> ///////////////////////////////////////////////////////////////////////////////
> void testEncodeCharacterStringValueNonEmptyValue() {
>  String value = "abc";
>
> System::Registry().replaceProperty("plugins.writer.text.file.outputbuffersize",
> "1");
>
>  // we want to see the writer.write(value) method called exactly
> once, with value "abc"
>  ASN::Plugins::Writer::Text::Stream::MockedWriter writer;
>  EXPECT_CALL(writer, write(Eq(String("abc")))).Times(1);
>  m_encodingControl->encodeCharacterStringValue(value, writer);
> }
>
> The writer and its write method are mocked.
>
> The above test works, if I change the expected result to "abd" it
> fails, so everything works as expected. What I am wondering about is
> how does google mock compare the two objects?

Eq() uses the == operator to compare the values:

http://www.google.com/codesearch#lUXKKIfQdpU/trunk/include/gmock/gmock-matchers.h&q=GMOCK_IMPLEMENT_COMPARISON_MATCHER_%20package:http://googlemock%5C.googlecode%5C.com&l=701

> Both are of type String,
> which is a class inside our framework that wraps certain string
> implementations.
>
> I would like google mock to use the operator== or compareTo
> implementation of the String class for the matching, but debugging
> tells me none of those methods get called.

Are you sure == isn't called?

> So how does google mock compare the two objects via Eq? I'm worried
> simply comparing the whole objects may fail the test under some
> cirumstances.
>

--
Zhanyong

Steffen

unread,
Sep 27, 2011, 4:52:34 PM9/27/11
to Google C++ Mocking Framework


On 27 Sep., 20:41, Zhanyong Wan (λx.x x) <w...@google.com> wrote:
> > I would like google mock to use the operator== or compareTo
> > implementation of the String class for the matching, but debugging
> > tells me none of those methods get called.
>
> Are you sure == isn't called?

Well, the VS2010 debugger didn't stop at the breakpoints, so I assumed
it wouldn't work. Could be a problem with the debugger, should checked
it again after a full rebuild of the projects.

I found a much better solution already though, by getting a copy of
the arguments via SaveArg. That way I have full freedom of asserting
single member variables of the paramters after the call.

Thanks for the help!

Keith Ray

unread,
Sep 29, 2011, 2:43:16 PM9/29/11
to Steffen, Google C++ Mocking Framework
I would expect your operator== function for your string class to be invoked, if it exists.

What compiler/IDE are you using?

You should also be able to specify the equivalent call:

EXPECT_CALL(writer, write(String("abc")));

taking advantage of defaults.

Be careful about your namespaces... google test has a String class in its "internal" namespace.

Reply all
Reply to author
Forward
0 new messages