17

我正在上课operator[],像这样:

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

如何使用谷歌模拟框架为这个方法创建一个模拟类?

我试过这个:

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
4

1 回答 1

21

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); }
}; 
于 2011-06-27T12:24:52.527 回答