如何在C 中更改静态方法行为
How can I change a static method behavior in C++?
本文关键字:静态方法 更新时间:2023-10-16
是否有一种方法可以修改静态方法的行为以返回不同的参数?
我正在使用gmock进行模拟,但在这种情况下,我无法更改我的代码,方法必须保持静态
例如
class MyClass
{
public:
static int GetSomething()
{
return -1;
}
};
我需要返回正数的方法
在这种情况下,您的选项有限,但是如果仅是一个模拟,则只需使该方法返回静态变量而不是硬编码值即可。
class MyClass
{
static int somethingValue;
public:
static int GetSomething()
{
return somethingValue;
}
static void SetSomething(int value)
{
somethingValue = value;
}
};
int MyClass::somethingValue = -1;
免责声明 - 我在单位测试公司打字。您可以通过我们的API轻松更改方法的行为,例如:
WHEN_CALLED(MyClass::GetSomething()).Return(15);
这样,在您的所有测试中,GetSomehting
都将返回15。它也将用于非静态方法。
相关文章:
- 尝试了解在导入的静态方法上使用删除方法时的错误
- 如何通过命名空间调用非静态方法
- 如何在没有实例的情况下获取非静态方法的类型?
- 调用从模板派生的类的静态方法,而不指定模板
- 如何在 c++ 中异步调用静态方法?
- C++ 将静态方法转换为简单方法
- 从另一个标头中的标头调用静态方法
- C++中静态方法的局部变量范围
- 如何启用使用另一个类的静态方法的模板函数的自动推导,从而消除冗长的调用方代码
- 使用本机 JNI 静态方法实现C++ Java 运行时错误
- 当只有静态方法受到影响时,如何解决C++中的链接器错误?
- 静态变量在同一个翻译单元中被静态方法使用时是否保证被初始化?
- 为什么链接器报告全局函数的乘法定义符号,而不是类静态方法
- 从部分专用模板方法调用模板非静态方法
- 如何使用 c++ 在 cocos2dx 中定义非静态方法
- 使用 CMake 的静态方法链接错误
- 在子类上调用模板化静态方法时获取类的类型名
- 仅具有公共静态方法的帮助程序类
- 调用没有主体的未实现静态方法
- 如何从C 的朋友类访问私有静态方法