我具有以下结构:
struct can_frame {
canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
__u8 can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */
__u8 __pad; /* padding */
__u8 __res0; /* reserved / padding */
__u8 __res1; /* reserved / padding */
__u8 data[CAN_MAX_DLEN] __attribute__((aligned(8)));
};
我通过引用将结构传递给模拟函数:
MOCK_METHOD1(write, int(can_frame* frame));
如果通过结构在数据字段中具有给定的数字,我想进行匹配:
EXPECT_CALL(*canSocketMock_, write(**if the value of can_frame->data[1] is equal to 10, else assert false**))).WillOnce(Return(16));
我试图结合Pointee,Field和ArrayElement匹配器,但是未能创建我想要的东西。匹配器的语法对我来说有点令人困惑。
编辑: 测试:
TEST_F(SchunkDeviceShould, applyBreakWritesRightMessage) {
ASSERT_NO_THROW(sut_.reset(new SchunkDevice(
canSocketMock_, 3)));
can_frame frame;
EXPECT_CALL(*canSocketMock_, write(FrameDataEquals(&frame, 1, CMD_STOP)))).WillOnce(Return(16));
ASSERT_TRUE(sut_->applyBreak());
}
我们调用的函数:
bool SchunkDevice::applyBreak() {
can_frame frame;
frame.can_id = MESSAGE_TO_DEVICE+canID_;
frame.can_dlc = 0x02;
frame.data[0] = frame.can_dlc - 1;
frame.data[1] = CMD_STOP;
if (int len = socket_->write(&frame) != 16) {
return false;
}
return true;
}
测试结果:
Unexpected mock function call - taking default action specified at:
/home/../SchunkDeviceTests.cpp:47:
Function call: write(0x7ffc27c4de60)
Returns: 16
Google Mock tried the following 1 expectation, but it didn't match:
/home/../SchunkDeviceTests.cpp:457: EXPECT_CALL(*canSocketMock_, write(FrameDataEquals(&frame, 1, CMD_STOP)))...
Expected arg #0: frame data equals (0x7ffc27c4def0, 1, 145)
Actual: 0x7ffc27c4de60
Expected: to be called once
Actual: never called - unsatisfied and active
答案 0 :(得分:0)
您可以定义custom matcher来检查结构内容。
MATCHER_P2(FrameDataEquals, index, value, "") { return (arg->data[index] == value); }
然后您将像这样使用它:
EXPECT_CALL(mock, write(FrameDataEquals(1, 10))).WillOnce(Return(16));