小编Dan*_*iel的帖子

如何将gmock函数分配给特定的函数指针?

我正在使用C ++在Gtest和Gmock上对两个dll进行单元测试:
A.dll和B.dll,都是用C编写的,我无法修改它们。

A.dll init函数通过函数指针将B.dll函数用作参数。我想模拟B的功能(因为它们是依赖于硬件的)。

我为A.dll创建了一个测试夹具类,该类可动态加载函数initcalc。以下代码提供了所需功能的快速概述:

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)

c c++ function-pointers googlemock gmock

3
推荐指数
1
解决办法
193
查看次数

标签 统计

c ×1

c++ ×1

function-pointers ×1

gmock ×1

googlemock ×1