如何使用运算符[]创建模拟类
How to create a mock class with operator[]?
我有一个operator[]
类,如下所示:
class Base
{
public:
virtual ~Base(){}
virtual const int & operator[]( const unsigned int index ) const = 0;
};
如何使用google mock框架为该方法创建mock类?
我试过这个:
class MockBase : public Base
{
public:
MOCK_CONST_METHOD1( operator[],
const int& ( const unsigned int )
);
};
但这会产生下一个错误:
error: pasting "]" and "_" does not give a valid preprocessing token
error: pasting "]" and "_" does not give a valid preprocessing token
error: pasting "]" and "_" does not give a valid preprocessing token
error: pasting "]" and "_" does not give a valid preprocessing token
MOCK_METHOD#
宏无法在运算符上工作。这个消息中给出的解决方案说要创建一个常规的模拟方法:
class Base {
public:
virtual ~Base () {}
virtual bool operator==(const Base &) = 0;
};
class MockBase: public Base {
public:
MOCK_METHOD1(Equals, bool(const Base &));
virtual bool operator==(const Base & rhs) { return Equals(rhs); }
};
相关文章:
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 无法创建抽象类的实例
- 如何使用"derived input class"创建派生类?
- 如何创建一个类,以便向量工作 std::vector<MyClass<int>> v{ 1,2,3 };
- 如何使用 std::make_shared 创建基类类型的智能指针?
- 如何使用模板化类创建其他类的实例?
- 如何为定义的情况创建子类?
- 创建子类的多个实例,其中只有一个超类实例
- 与简单地创建派生类指针相比,将基类绑定到派生类有什么优点?
- 创建一个类来访问和指定向量类型,并构建一个获取位置并为其分配区域的类
- 创建基类指针的向量并将派生类对象传递给它(多态性)
- 在基类中创建派生类的对象
- 成功创建c++类和对象
- 无法创建一个类的多个实例
- 在父类函数中创建子类 - 可能吗?
- 创建Mesh类并实现不会生成模型OpenGL
- 在谷歌测试中创建模拟类的向量
- googlemock:无法创建模拟类的对象
- 如何模拟QTreeWidget itemClicked信号而不创建派生类
- 如何使用运算符[]创建模拟类