【发布时间】:2014-09-17 12:00:16
【问题描述】:
我很难保存我的模拟接收到的指针参数。
#define SIZE_OF_DATA
typedef struct {
uint32_t someValue1;
uint16_t someValue2;
// other values here
} LargeStruct;
class SomeClass {
public:
// assume sendData is a generic function where data is actually pointer to a LargeStruct
void sendData(const uint8_t* data, const uint16_t size);
}
class MockClass : public SomeClass {
public:
MOCK_METHOD2(sendData, void(const uint8_t*, const uint16_t));
};
我想将第一个参数保存到sendData(指针)并查看它指向的数据(它指向一个很大的结构,所以我不想按值复制):
TEST(SomeFixture, sendData_checkSentDataIsValid) {
MockClass mock;
const uint8_t *pData;
EXPECT_CALL(mock, sendData(_, SIZE_OF_DATA)).WillOnce(SaveArg<0>(&pData));
// do something here that calls sendData()
// hopefully data should point to the same data that was passed in to the method
LargeStruct *ls = (LargeStruct *)pData;
// now verify that the data is ok...
// some expectations here
EXPECT_EQ(SOMEVALUEIWANT, ls->someValue1);
}
但是,pData 指向的数据是错误的——我想我似乎是将指针值保存到结构中,而不是保存指针。
我认为问题在于我传递给 SaveArg 的变量,但我似乎无法在编译并给出正确答案的版本中得到它。请大家指点一下?
【问题讨论】:
标签: c++ googlemock