开发者

How to create a mock class with operator[]?

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: pasti开发者_开发百科ng "]" 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜