我正在研究一个古老的C++ C++代码库,我一直在为缺少它们的组件创建单元测试的过程。
为了测试,模仿这些类的功能的正确方法是什么?目前我使用的是虚拟函数,在我的测试模块中,我从基本的“生产”类派生一个模拟类,并根据需要重写功能。
举个例子:
生产.cpp
class Production {
protected:
int Servers = 0;
public:
virtual bool IsValInRegistry(const std::string& regVal);
};
bool Production::IsValInRegistry(const std::string& regVal)
{
HRESULT hr = GOOD;
hr = WinSysCallToRegistry(regVal);
if (HR)
return true;
else
return false;
}
class Mock : public Production {
public:
bool IsValInRegistry(const std::string& regVal) override final;
};
Mock::IsValInRegistry(const std::string& regVal)
{
return true;
}
这是正确的方法吗?我担心我介绍的人太多了
virtual
功能,我可能会看到一个性能冲击,我想避免。如果这个
virutal