1

関数呼び出しに「依存性注入」を実行しようとする単体テスト ( cpputest 内) を作成しています。これは、単体テストがテスト対象のファイル内に配置された実際の関数を呼び出す必要がある場合、関数呼び出しを「偽の」実装にリダイレクトする必要があることを意味します。実際には、関数ポインタを実際の関数に割り当て、「偽の実装」で上書きしています。以下のように構成されています。

============ myfile.h =========================
 int8 my_function_FAKE (int8 address)     // Declaration of my_function_FAKE
==============================================

================= myfile.c ====================
#include "myfile.h"

static int8 my_function (int8 address)   // The original function
{
   return value;
}


#IF DEFINED (UNIT_TEST)
int8 my_function_FAKE (int8 address)   // the "fake" of the original function 
{
   switch (address)
   {
      case 10: return 11
      case 20: return 21
      case 30: return 31
   }
}
#ENDIF

======================TEST ENVIRONMENT =======================
==============================================================

========FAKE.h===============
extern int8(*Function)(int8);
=========================

 ========FAKE.c==========
 #include "myfile.h"
 #include "FAKE.h"

 int8 (*Function)(int8) = my_function;
 =========================

=======Unit Test File======
Function = my_function_FAKE; // Injecting the fake implementation within unit test file
===========================

コンパイラ エラーが発生します。

FAKE.c: error C2065: 'my_function' : undeclared identifier
FAKE.c: error C2099: 'my_function' : initializer is not a constant 

私はすでにいくつかの組み合わせを試しましたが、毎回同じエラーです。解決策はおそらく単純ですが、私はそれを見落としています。それで、私はここで何が間違っていますか?

4

1 に答える 1