Property and shared_ptr

410 views
Skip to first unread message

An Ky

unread,
Dec 18, 2013, 8:04:50 AM12/18/13
to googl...@googlegroups.com
Hi,

I'm trying to mock a method call where I want to check a specific property of the passed object.

struct MockExporter : public Exporter
{
  MOCK_METHOD1(saveChannel, void(ChannelPtr));
}

with

class Channel
{
...
  QString name() const { return _name; }
...
};

typedef std::shared_ptr<Channel> ChannelPtr;


Using this in an expectation like

EXPECT_CALL(*mockExporter, saveChannel(Property(&Channel::name, Eq("RainflowMatrixX_Test2"))));

results in the following compiler error

\gmock\gmock.h:6364: Fehler: C2664: 'bool testing::internal::PropertyMatcher<Class,PropertyType>::MatchAndExplainImpl(testing::internal::false_type,const Class &,testing::MatchResultListener *) const': Konvertierung des Parameters 2 von 'const ChannelPtr' in 'const Channel &' nicht m”glich
with
[
    Class=Channel,
    PropertyType=QString
]
Ursache: Konvertierung von 'const ChannelPtr' in 'const Channel' nicht m”glich
Kein benutzerdefinierter Konvertierungsoperator verf gbar, der diese Konvertierung durchf hren kann, oder der Operator kann nicht aufgerufen werden
D:\VirtualBox\Shared\Projekte\monalysis\3rd_party\testing\gmock/gmock.h(4961): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "bool testing::internal::PropertyMatcher<Class,PropertyType>::MatchAndExplain<T>(const T &,testing::MatchResultListener *) const".
with
[
    Class=Channel,
    PropertyType=QString,
    T=ChannelPtr
]
D:\VirtualBox\Shared\Projekte\monalysis\3rd_party\testing\gmock/gmock.h(4960): Bei der Kompilierung der  Klassen-template der bool testing::PolymorphicMatcher<Impl>::MonomorphicImpl<T>::MatchAndExplain(T,testing::MatchResultListener *) const-Memberfunktion
with
[
    Impl=testing::internal::PropertyMatcher<Channel,QString>,
    T=ChannelPtr
]
D:\VirtualBox\Shared\Projekte\monalysis\3rd_party\testing\gmock/gmock.h(4943): Siehe Verweis auf die Instanziierung der gerade kompilierten Klassen-template "testing::PolymorphicMatcher<Impl>::MonomorphicImpl<T>".
with
[
    Impl=testing::internal::PropertyMatcher<Channel,QString>,
    T=ChannelPtr
]
.\FrequencyDistributionWorkflowTest.cpp(382): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "testing::PolymorphicMatcher<Impl>::operator testing::Matcher<T>(void) const<A1>".
with
[
    Impl=testing::internal::PropertyMatcher<Channel,QString>,
    T=ChannelPtr,
    A1=ChannelPtr
]

Sorry for being in german, rough translation is that the second argument of MatchAndExplainImpl cannot be converted from const ChannelPtr to const Channel&. 
I'm stuck here having no clue how to solve it. Should the shared_ptr argument not be used just like a plain pointer as stated in the docs?

Thanks for any help.

Greets 
An Ky

Vlad Losev

unread,
Jan 14, 2014, 2:46:24 PM1/14/14
to Google C++ Mocking Framework
The problem here is that testing::Field and testing::Property only work for object references and raw object pointers. You should be able to write a custom matcher that does the same, though:

MATCHER_P(HasName, name, "") { return arg->name() == name; }
...
EXPECT_CALL(*mockExporter, saveChannel(HasName("RainflowMatrixX_Test2")));


HTH,
Vlad



--
 
---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googlemock/36019b6a-0a12-4184-97dc-5e7f084541c2%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages