Googlemock:如何验证对象中数组中的元素?

发布于 2024-10-19 03:10:56 字数 841 浏览 3 评论 0原文

我有一个小类:

struct Command
{
    uint8_t cmdId;
    uint8_t len;
    uint8_t payload[MAX_PAYLOAD];
};

我想使用 googlemock 期望仅验证有效负载的前两个元素。我无法使用 ElementsAreArray,因为它会检查有效负载和期望的长度是否相同。到目前为止,我的期望是这样的:

Command cmd;
cmd.cmdId = 0xD3;
cmd.len = 2;
cmd.payload[0] = 'm';
cmd.payload[1] = 'l';

EXPECT_CALL(mockQueue,
        sendFromIsr(Pointee(AllOf(
                Field(&Command::cmdId, Eq(0xD3)),
                Field(&Command::len, Eq(2)),
                //Field(&BirdCommand::payload, ElementsAreArray(cmd.payload, 2)) !<-- Doesn't work
        ))))
        .WillOnce(Return(true));

有什么想法吗?模拟类如下所示:

template <typename T>
class MockQueue : public Queue<T>
{
public:
    MOCK_METHOD1_T(sendFromIsr, bool(T &item));
};

I have a small class:

struct Command
{
    uint8_t cmdId;
    uint8_t len;
    uint8_t payload[MAX_PAYLOAD];
};

And I want to verify only the first two elements of the payload using a googlemock expectation. I can't use ElementsAreArray because that checks that the lengths of the payload and the expectation are the same. So far I have an expectation that looks like this:

Command cmd;
cmd.cmdId = 0xD3;
cmd.len = 2;
cmd.payload[0] = 'm';
cmd.payload[1] = 'l';

EXPECT_CALL(mockQueue,
        sendFromIsr(Pointee(AllOf(
                Field(&Command::cmdId, Eq(0xD3)),
                Field(&Command::len, Eq(2)),
                //Field(&BirdCommand::payload, ElementsAreArray(cmd.payload, 2)) !<-- Doesn't work
        ))))
        .WillOnce(Return(true));

Any ideas? The mock class looks like this:

template <typename T>
class MockQueue : public Queue<T>
{
public:
    MOCK_METHOD1_T(sendFromIsr, bool(T &item));
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幻梦 2024-10-26 03:10:56

这是什么问题:

EXPECT_CALL( mockQueue, sendFromIsr( Ref( cmd ) )
        .WillOnce( Return( true ) );

由于命令是 POD,因此即使它复制命令(在本例中不是这样),它也应该可以工作。


编辑

由于命令是POD,因此您需要对其进行零初始化(或者在创建该对象时清除该对象占用的内存。在您的示例中:

Command cmd = Command();

那么您的对象将不会未初始化,并且比较不应该失败。

What is wrong with this :

EXPECT_CALL( mockQueue, sendFromIsr( Ref( cmd ) )
        .WillOnce( Return( true ) );

?

Since the Command is POD, it should work even if it copies the command (which it doesn't in this case).


EDIT

Since the Command is POD, you need to zero initialize it (or clear the memory occupied by such object when you create it. In your example :

Command cmd = Command();

Then your object will not be uninitialized and the comparison should not fail.

卸妝后依然美 2024-10-26 03:10:56

我通过 googlemock 邮件列表从 Piotr Gorak 收到了此解决方案,此处讨论

MATCHER_P2(CheckFirstTwo, first, second, "Checks the first two elements of an array")
{
    return arg[0] == first && arg[1] == second;
}

在测试中我这样验证:

EXPECT_CALL(mockQueue, sendFromIsr(
        Pointee(AllOf(
            Field(&BirdCommand::cmdId, Eq(0xD3)),
            Field(&BirdCommand::len, Eq(2)),
            Field(&BirdCommand::payload, CheckFirstTwo('m', 'l'))
        ))
));

我最喜欢这个解决方案,因为它不需要我过度指定测试或对生产代码中的数据结构进行不必要的零初始化。

I have received this solution from Piotr Gorak via the googlemock mailing list, discussion here.

MATCHER_P2(CheckFirstTwo, first, second, "Checks the first two elements of an array")
{
    return arg[0] == first && arg[1] == second;
}

And in the test I verify like this:

EXPECT_CALL(mockQueue, sendFromIsr(
        Pointee(AllOf(
            Field(&BirdCommand::cmdId, Eq(0xD3)),
            Field(&BirdCommand::len, Eq(2)),
            Field(&BirdCommand::payload, CheckFirstTwo('m', 'l'))
        ))
));

I like this solution the most because it does not require me to over-specify the test or zero initialize the data structure in my production code unnecessarily.

℡寂寞咖啡 2024-10-26 03:10:56

你尝试过吗

EXPECT_CALL(mockQueue,
        sendFromIsr(Pointee(AllOf(
                Field(&Command::cmdId, Eq(0xD3)),
                Field(&Command::len, Eq(2)),
                Field(&Command::payload[0], Eq('m')),
                Field(&Command::payload[1], Eq('l'))
        ))))
        .WillOnce(Return(true));

Have you tried

EXPECT_CALL(mockQueue,
        sendFromIsr(Pointee(AllOf(
                Field(&Command::cmdId, Eq(0xD3)),
                Field(&Command::len, Eq(2)),
                Field(&Command::payload[0], Eq('m')),
                Field(&Command::payload[1], Eq('l'))
        ))))
        .WillOnce(Return(true));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文