我正在使用C ++在Gtest和Gmock上对两个dll进行单元测试:
A.dll和B.dll,都是用C编写的,我无法修改它们。
A.dll init函数通过函数指针将B.dll函数用作参数。我想模拟B的功能(因为它们是依赖于硬件的)。
我为A.dll创建了一个测试夹具类,该类可动态加载函数init和calc。以下代码提供了所需功能的快速概述:
class TestFixture : public ::testing::Test {
// dynamically loads functions from A.dll and assigns
// them to function pointers init_ptr and calc_ptr.
};
// function pointer typedef for use with B.dll's functions
typedef int (*funcPtr)(int, int);
// Loaded from A.dll
void init(funcPtr func1, funcPtr func2) {
// internally set functions in A.dll to be used in calculate
}
// Loaded from A.dll
int calculate(int a, int b) {
// …Run Code Online (Sandbox Code Playgroud)