我有一个代码库,其中我实现的许多类都派生自我公司其他部门提供的类。与这些其他部门合作通常具有工作关系,就好像他们是第三方中间件供应商一样。
我正在尝试在不修改这些基类的情况下编写测试代码。但是,由于缺少接口,创建有意义的测试对象存在问题:
//ACommonClass.h
#include "globalthermonuclearwar.h" //which contains deep #include dependencies...
#include "tictactoe.h" //...and need to exist at compile time to get into test...
class Something //which may or may not inherit from another class similar to this...
{
public:
virtual void fxn1(void); //which often calls into many other classes, similar to this
//...
int data1; //will be the only thing I can test against, but is often meaningless without fxn1 implemented
//...
};
我通常会提取一个界面并从那里工作,但由于这些是“第三方”,我无法提交这些更改。
目前,我创建了一个单独的文件,其中包含在需要知道的基础上在第三方提供的基类头文件中定义的函数的假实现,如“使用遗留代码”一书中所述。
我的计划是继续使用这些定义,并为我需要的每个第三方类提供替代测试实现:
//SomethingRequiredImplementations.cpp
#include "ACommonClass.h"
void CGlobalThermoNuclearWar::Simulate(void) {}; // fake this and all other required functions...
// fake implementations for otherwise undefined functions in globalthermonuclearwar.h's #include files...
void Something::fxn1(void) { data1 = blah(); } //test specific functionality.
但在我开始这样做之前,我想知道是否有人尝试在类似于我的代码库上提供实际对象,这将允许创建新的测试特定类来代替实际的第三方类。
请注意,所有有问题的代码库都是用 C++ 编写的。