我刚开始进行单元测试(使用 BOOST 框架进行测试,但对于模拟我必须使用 Google Mock)并且我遇到了这种情况:
class A
{
A(){}
virtual int Method1(int a, int b){return a+b;}
};
class B
{
static int Method2(int a, int b){ return A().Method1(a,b);}
};
是否可以对 B 类进行测试,以这种方式使用模拟 Method1 而不是真实方法,但不能更改 B 类?我知道这很容易:
class B
{
B(A *a):a_in_b(a){}
static int Method2(int a, int b){return a_in_b->Mehod1();}
A *a_in_b;
};