More than one active expectation on a call with different subclasses expected to be passed as arguments

2,629 views
Skip to first unread message

Brian Forney

unread,
Jul 23, 2013, 12:19:00 PM7/23/13
to googl...@googlegroups.com
Hi all,

I'm trying to create two expectations for a mock object's SendMessage() method. The expectations differ in the type of pointer passed as the second argument. The two expectations specify different subclass types for this second argument. When the test containing the two expectations is run, one expectation is over-saturated and the other is never called and unsatisfied. I've tried various different specifications of the expectations with no success. My guess is I am missing some piece of knowledge about how Google Mock works.

More concretely, I have a interface that I have mocked:

class NetworkEndpointInterface
{
public:
virtual ~NetworkEndpointInterface() {}

virtual void SendMessage(
NetworkAddressInterface *ipDestAddress,
MessageInterface *ipMessage
) = 0;

protected:
NetworkEndpointInterface() {}
};

class NetworkEndpointMock: public NetworkEndpointInterface
{
public:
NetworkEndpointMock();
virtual ~NetworkEndpointMock();

MOCK_METHOD2(SendMessage, void(NetworkAddressInterface *ipDestAddress, MessageInterface *ipMessage));
};

Then, I have a MessageInterface interface and two subclasses that implement MessageInterface:

class MessageInterface
{
public:
virtual ~MessageInterface() {}

        // ....

protected:
MessageInterface() {}
};

class MessageA : public MessageInterface
{
public:
virtual ~MessageA();
        MessageA();
        // ....
};

class MessageB : public MessageInterface
{
public:
virtual ~MessageB();
        MessageB();
        // ....
};

Here's the test code:

        // ...

NetworkAddressMock netAddress1(0);
NetworkEndpointMock networkEndpoint();

        // ...

EXPECT_CALL(
networkEndpoint,
SendMessage(
&netAddress1,
::testing::MatcherCast<MessageInterface *>(
::testing::SafeMatcherCast<MessageA *>(
::testing::Pointee(
   ::testing::_
)
)
)
)
).Times(
1
);
EXPECT_CALL(
networkEndpoint,
SendMessage(
&netAddress1,
   ::testing::MatcherCast<MessageInterface *>(
    ::testing::SafeMatcherCast<MessageB *>(
    ::testing::Pointee(
    ::testing::_
    )
    )
   )
)
).Times(
3
);

        // ...

Here are the error messages from Google Mock:

myTest.cpp(397): error: Mock function called more times than expected - taking default action specified at:
networkEndpointMock.cpp(45):
    Function call: SendMessage(000000000023ED30, 000000000082A2D0)
         Expected: to be called 3 times
           Actual: called 4 times - over-saturated and active
myTest.cpp(320): error: Actual function call count doesn't match EXPECT_CALL(networkEndpoint, SendMessageA( &netAddress1, ::testing::MatcherCast<MessageInterface *>( ::testing::SafeMatcherCast<MessageA *>( ::testing::Pointee( ::testing::_ ) ) ) ))...
         Expected: to be called once
           Actual: never called - unsatisfied and active

It seems the second expectation is matching for MessageA * and MessageB *.

Any suggestions on how to achieve this type of matching?

Keith Ray

unread,
Jul 23, 2013, 12:31:04 PM7/23/13
to Brian Forney, googl...@googlegroups.com
The DECLARATION of the mocked method only takes one type (MessageInterface *), and therefore can't distinguish between the subclasses being passed into the mocked method.

You could do something like this:

class NetworkEndpointMock: public NetworkEndpointInterface
{
public:
NetworkEndpointMock();
virtual ~NetworkEndpointMock();

virtual void SendMessage(NetworkAddressInterface *ipDestAddress, MessageInterface *ipMessage))
{
MessageA * am = dynamic_cast<MessageA *>(ipMessage);
MessageB * bm = dynamic_cast<MessageB *>(ipMessage);
if (am) SendMessageA(...);
else if (bm) SendMessageB(...);
}

private
MOCK_METHOD2(SendMessageA, void(NetworkAddressInterface *ipDestAddress, MessageA *ipMessage));
MOCK_METHOD2(SendMessageB, void(NetworkAddressInterface *ipDestAddress, MessageB *ipMessage));
};


--
 
---
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/7bb62954-d4cc-4c41-af17-a7bcecdc2a36%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages