Search code examples
c++boostgooglemock

compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter


I'm having a problem with EXPECT_CALL method, when trying to do this :

boost::program_options::variables_map vm;  
MyMock mock;  
EXPECT_CALL(mock, MyMethod(vm)).WillOnce(Return(L""));  

MyMethod looks like this :

std::wstring MyMethod(const boost::program_options::variables_map &vm)

When compiling I got errors :

Error   17  error C2676: binary '==' : 'const boost::program_options::variable_value' does not define this operator or a conversion to a type acceptable to the predefined operator C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\utility   

Error   10  error C2784: 'bool std::operator ==(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const boost::program_options::variable_value'    C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\utility

And a few more similar errors.


Solution

  • To use EXPECT_CALL, your class needs to support operator==. Since boost::program_options::variables_map has no operator==, you can not use it like that.

    You could define you own matcher for boost::program_options::variables_map, however I would advise you to pass it to a function where you would check expected values (or you can ignore, set flag, or do whatever you please). Something like this :

    int called = 0;
    void foo( const boost::program_options::variables_map &)
    {
      ++ called;
    }
    

    In the test :

    boost::program_options::variables_map vm;  
    MyMock mock;
    called = 0;
    EXPECT_CALL(mock, MyMethod(_)).WillOnce(Invoke(&foo));  
    // assert that called is 1