0
我有一個模擬來模擬從設備讀取數據。問題我不知道如何設置buf值來模擬讀數。我想模擬sensor.read(buf,sizeof(int))函數中的buf值。有沒有辦法做到這一點?硒我的代碼如下:谷歌嘲笑從設備讀取
模擬類:
class Device
{
public:
virtual int Read(char *buf, size_t size) = 0;
};
class MockDevice: public Device{
public:
MOCK_METHOD2(Read, int(char *buf, size_t size));
};
我的類:
class Sensor
{
private:
Device *dev;
public:
Sensor(Device *device): dev(device);
int DoRead(char *buf, size_t size){
dev->Read(buf,size);
// How can i set the buf here?
}
}
測試:
TEST(DeviceReadTest, Read)
{
char buf[10] = {0xAA}
MockDevice *mockDevice = new MockDevice();
EXPECT_CALL(*mockDevice, Read(_,_)).Times(1).WillOnce(Return(10));
Sensor sensor(mockDevice);
sensor.DoRead(buf,sizeof(buf)); // I wanna pass the buf content to the mock function. Is it possible?
}