你模拟它就像你模拟任何其他类一样。让 RAII 类的构造函数处理它。
class MyInterface
{
virtual void MyApiFunction(int myArg)
{
::MyApiFunction(myArg);
}
};
class MyRAII : boost::noncopyable //Shouldn't be copying RAII classes, right?
{
MyInterface *api;
public:
MyRAII(MyInterface *method = new MyInterface)
: api(method)
{
//Aquire resource
}
~MyRAII()
{
//Release resource
delete api;
}
};
class MockInterface : public MyInterface
{
MOCK_METHOD1(MyApiFunction, void(int));
};
TEST(Hello, Hello)
{
std::auto_ptr<MockInterface> mock(new MockInterface);
EXPECT_CALL(*mock, ....)...;
MyRAII unitUnderTest(mock.release());
}