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?