Google 模拟从设备读取
Google mocking reading from device
我有一个模拟来模拟从设备读取数据。问题 我不知道如何设置 buf 值来模拟读数。我想模拟 sensor.read(buf,sizeof(int)) 函数中的 buf 值。有什么办法吗?请在下面查看我的代码:
模拟 class:
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:
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?
}
像这样的东西应该可以工作:
const char read_result[] = "abc";
EXPECT_CALL(*mockDevice, Read(_,_))
.Times(1)
.WillOnce(DoAll(
SetArrayArgument<0>(
read_result,
read_result + strlen(read_result)),
Return(strlen(read_result)));
您可以在 https://code.google.com/p/googlemock/wiki/CheatSheet#Actions
找到可能的操作的完整列表
我有一个模拟来模拟从设备读取数据。问题 我不知道如何设置 buf 值来模拟读数。我想模拟 sensor.read(buf,sizeof(int)) 函数中的 buf 值。有什么办法吗?请在下面查看我的代码:
模拟 class:
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:
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?
}
像这样的东西应该可以工作:
const char read_result[] = "abc";
EXPECT_CALL(*mockDevice, Read(_,_))
.Times(1)
.WillOnce(DoAll(
SetArrayArgument<0>(
read_result,
read_result + strlen(read_result)),
Return(strlen(read_result)));
您可以在 https://code.google.com/p/googlemock/wiki/CheatSheet#Actions
找到可能的操作的完整列表