Reputation: 647
I had EXPECT_CALL
passing on my tests, until I added a header to wrap the unsigned char *
I was testing for. I understand that the test is falling, since it isn't the same unsigned char *
as expected. I'm just not sure how to redesign my tests, so the unsigned char *
assigned to causes the test to pass.
My test(With initialization of variables left out):
EXPECT_CALL(networkConnectionMock, Transmit(packetData, dataSize));
packetManager.Update(0.01f);
packetManager.Send(packetData, dataSize);
The function under test:
void PacketManager::Send(unsigned char *packetData, int packetSize)
{
if(this->secondSinceLastUpdate <= 1.0f)
packetsSent++;
else
{
packetsSent = 0;
this->secondSinceLastUpdate = 0.0f;
}
std::stringstream convert;
convert << htonl((uint32_t)packetSize);
Header *header = new Header;
header->name = "PKT_" + convert.str();
header->packetSize = packetSize;
header->packetData = packetData;
unsigned char * pktHeader = new unsigned char[sizeof(Header)];
pktHeader = (unsigned char*)header;
PKT_Header = pktHeader; // tried assigning to public variable,
// but would be assigned to after EXPECT_CALL is setup.
int totalPacketSize = packetSize; // rewrite to loop through que
if(header->packetSize <= 800 && totalPacketSize <= 1024 && packetsSent <= 30)
networkConnection->Transmit(pktHeader, packetSize);
}
gmock output:
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from PacketManager
[ RUN ] PacketManager.SendsPacketsUnder1024
unknown file: Failure
Unexpected mock function call - returning directly.
Function call: Transmit(0x2096c10, 100)
Google Mock tried the following 1 expectation, but it didn't match:
/home/zerophase/ClionProjects/PacketManager/tests/basic_tests/basic_check.cpp:20: EXPECT_CALL(networkConnectionMock, Transmit(packetData, dataSize))...
Expected arg #0: is equal to 0x2096560
Actual: 0x2096c10
Expected: to be called once
Actual: never called - unsatisfied and active
/home/zerophase/ClionProjects/PacketManager/tests/basic_tests/basic_check.cpp:20: Failure
Actual function call count doesn't match EXPECT_CALL(networkConnectionMock, Transmit(packetData, dataSize))...
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] PacketManager.SendsPacketsUnder1024 (0 ms)
[----------] 1 test from PacketManager (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] PacketManager.SendsPacketsUnder1024
Is EXPECT_CALL still what I want to use to test for Transmit
being called?
Upvotes: 1
Views: 3667
Reputation: 6982
You are trying to match the call to Transmit
with packetData
as first parameter, but the call being made in PacketManager::Send
is for pktHeader
, which is a (leaking) local variable. The mock correctly reports unmatched function call, and must also report a warning for uninteresting function call.
Upvotes: 0
Reputation: 2124
If you don't really care about the exact parameter being passed to Transmit
then you can use the _
keyword.
EXPECT_CALL(networkConnectionMock, Transmit(_, dataSize));
If you do care about the exact parameter then I would rearrange my code slightly so that instead of passing in a raw unsigned char*
into Send
, I would create a simple class that does the work you're doing in Send
and have that class be responsible for returning an unsigned char*
.
That way you can pass a mock object into your Send
method which gives you more control over what parameters are passed into Trasmit
.
Upvotes: 2