私が実装するクラスの多くが、会社の他の部門によって提供されるクラスから派生したコード ベースを持っています。これらの他の部門との共同作業は、多くの場合、サード パーティのミドルウェア ベンダーであるかのような関係を築いています。
これらの基本クラスを変更せずにテスト コードを記述しようとしています。ただし、インターフェイスがないため、意味のあるテスト オブジェクトを作成するには問題があります。
//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
//...
};
通常はインターフェイスを抽出してそこから作業しますが、これらは「サード パーティ」であるため、これらの変更をコミットすることはできません。
現在、「Working with Legacy Code」という書籍で説明されているように、必要に応じて、サードパーティが提供する基本クラス ヘッダーで定義されている関数の偽の実装を保持する別のファイルを作成しました。
私の計画は、これらの定義を引き続き使用し、必要なサードパーティ クラスごとに代替のテスト実装を提供することでした。
//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++ で記述されていることに注意してください。