2011-06-27 21 views
14

我有operator[]一类,像这样:如何用operator []创建一个模拟类?

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

我如何使用Google模拟框架此方法的模拟类?

我尝试这样做:

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 

回答

18

MOCK_METHOD#宏将不会在运营商工作。在this message给出的解决方案说创建一个嘲笑的常规方法:

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

谢谢。这工作正常 –

+0

这将如何工作的赋值运算符? – Mawg