Googlemock:如何验证对象中数组中的元素?
我有一个小类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是什么问题:
?
由于命令是 POD,因此即使它复制命令(在本例中不是这样),它也应该可以工作。
编辑
由于命令是POD,因此您需要对其进行零初始化(或者在创建该对象时清除该对象占用的内存。在您的示例中:
那么您的对象将不会未初始化,并且比较不应该失败。
What is wrong with this :
?
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 :
Then your object will not be uninitialized and the comparison should not fail.
我通过 googlemock 邮件列表从 Piotr Gorak 收到了此解决方案,此处讨论。
在测试中我这样验证:
我最喜欢这个解决方案,因为它不需要我过度指定测试或对生产代码中的数据结构进行不必要的零初始化。
I have received this solution from Piotr Gorak via the googlemock mailing list, discussion here.
And in the test I verify like this:
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.
你尝试过吗
Have you tried