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

関連する問題