1
我正在寫一個單元測試(在cpputest),我嘗試在函數調用中執行「依賴注入」。這意味着,當單元測試必須調用放置在被測文件內的實函數時,函數調用應該被重定向到「假」實現。實際上,我將函數指針分配給實際函數,並用「假實現」覆蓋它。它的構造如下:如何在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
我已經嘗試了一些組合,但是每一次同樣的錯誤。解決方案可能很簡單,但我忽略了它。那麼,我在這裏做錯了什麼?