如何解决 gmock EXPECT_CALL 失败,当变量传递给函数时无关紧要

How to solve gmock EXPECT_CALL failure, when variable passed to function doesn't matter

我已经 EXPECT_CALL 通过了我的测试,直到我添加了一个 header 来包装我正在测试的 unsigned char *。我知道测试正在下降,因为它与预期的 unsigned char * 不一样。我只是不确定如何重新设计我的测试,因此分配给的 unsigned char * 导致测试通过。

我的测试(省略了变量的初始化):

EXPECT_CALL(networkConnectionMock, Transmit(packetData, dataSize));

packetManager.Update(0.01f);
packetManager.Send(packetData, dataSize);

被测函数:

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 输出:

[==========] 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

EXPECT_CALL 仍然是我想用来测试 Transmit 被调用的东西吗?

您正在尝试将对 Transmit 的调用与 packetData 作为第一个参数匹配,但在 PacketManager::Send 中进行的调用是针对 pktHeader 的,这是一个(泄漏)局部变量。模拟正确报告不匹配的函数调用,并且还必须报告 uninteresting function call.

的警告

如果您真的不关心传递给 Transmit 的确切参数,那么您可以使用 _ 关键字。

EXPECT_CALL(networkConnectionMock, Transmit(_, dataSize));

如果您确实关心确切的参数,那么我会稍微重新安排我的代码,这样我就不会将原始 unsigned char* 传递给 Send,而是创建一个简单的 class完成您在 Send 中所做的工作,并让 class 负责返回 unsigned char*.

这样您就可以将模拟对象传递给您的 Send 方法,这样您就可以更好地控制传递给 Trasmit.

的参数。