How to create a mock class with operator[]?

泄露秘密 提交于 2019-12-08 14:47:35

问题


I am having a class with operator[], like this :

class Base
{
  public:
    virtual ~Base(){}
    virtual const int & operator[]( const unsigned int index ) const = 0;
};

How can I create a mock class using google mock framework for this method?

I tried this :

class MockBase : public Base
{
public:
  MOCK_CONST_METHOD1( operator[],
                      const int& ( const unsigned int )
                      );
};

but that produces next errors :

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

回答1:


The MOCK_METHOD# macros won't work on operators. The solution given in this message says to create a regular method for mocking:

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); }
}; 


来源:https://stackoverflow.com/questions/6492664/how-to-create-a-mock-class-with-operator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!