2011-06-27 86 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