Google Mock匹配数组

时间:2018-03-14 16:34:59

标签: c googletest gmock

我知道这已被多次询问,但没有一个答案适合我。 这是我想测试的模拟功能:

  MOCK_METHOD2(sendInternal, void(uint8_t data[], uint8_t size));

测试就像这样

    uint8_t expectedMessageData[3] = { 0x08, 0xda, 0xff };


    EXPECT_CALL(*serverFake, sendInternal(testing::_,3))
            .With(testing::Args<0, 1>(ElementsAre(0x08, 0xda, 0xff)))
            .Times(1);

但这会导致

  

预期的args:是一个元组,其字段(#0,#1)有2个元素   元素#0等于&#39; \ b&#39; (8),   元素#1等于&#39; \ xDA&#39; (218)              实际:不匹配,其字段(#0,#1)为(0x7fcfd9500590,&#39; \ x11&#39;(3)),其中包含3个元素

对我来说,似乎Gmock会比较params而不是数组的元素。

我甚至构建了一个自定义匹配器:

MATCHER_P2(HasBytes, bytes, size, "") {
    uint8_t * dataToCheck = arg;
    bool isMatch = (memcmp(dataToCheck, bytes, size) == 0);
    return isMatch;
}

我可以看到(在调试时)isMatch == true但测试仍然失败。

请帮忙!

1 个答案:

答案 0 :(得分:2)

首先,我没有重现你的问题。以下示例编译并通过测试:

class ServerFake {
  public:
    MOCK_METHOD2(sendInternal, void(uint8_t data[], uint8_t size));
};

// If only possible, I recommend you to use std::vector instead of raw array
class ServerFake2 {
  public:
    MOCK_METHOD1(sendInternal, void(std::vector<uint8_t> data));
};

MATCHER_P2(HasBytes, bytes, size, "") {
    // unnecessary assignment, I think ...
    uint8_t * dataToCheck = arg;
    bool isMatch = (memcmp(dataToCheck, bytes, size) == 0);
    return isMatch;
}

using ::testing::ElementsAre;

TEST(xxx, yyy) {

    ServerFake* serverFake = new ServerFake;
    ServerFake2* serverFake2 = new ServerFake2;

    uint8_t expectedMessageData[3] = { 0x08, 0xda, 0xff };
    std::vector<uint8_t> expectedMessageData2({ 0x08, 0xda, 0xff });

    EXPECT_CALL(*serverFake, sendInternal(HasBytes(expectedMessageData, 3), 3)).Times(1);
    // the code below compiles and passes as well! However, I didn't check why;
    // EXPECT_CALL(*serverFake, sendInternal(testing::_,3))
    //     .With(testing::Args<0, 1>(ElementsAre(0x08, 0xda, 0xff)))
    //     .Times(1);
    serverFake->sendInternal(expectedMessageData, 3);


    // much better, do like this!
    EXPECT_CALL(*serverFake2, sendInternal(ElementsAre(0x08, 0xda, 0xff))).Times(1);
    serverFake2->sendInternal(expectedMessageData2);


    delete serverFake;
    delete serverFake2;
}

其次,根据this topicElementsAre正式不支持C风格的数组。如果您真的无法更改方法签名,并且您真的希望在原始数组上使用ElementsAre,则可以使用该主题中提出的hack。欢呼声。