使用具有program_options :: variables_map作为参数的函数初始化EXPECT_CALL时的编译器错误

时间:2012-01-25 10:53:23

标签: c++ boost gmock

尝试执行此操作时,我遇到了EXPECT_CALL方法的问题:

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

MyMethod看起来像这样:

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

编译时出错:

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

还有一些类似的错误。

2 个答案:

答案 0 :(得分:1)

此外,您可以创建自己的谓词作为匹配器,因为我发现在尝试与boost匹配时我需要做。

请参阅here

在你的测试中:

using :testing::Return;
using ::testing::Truly;
EXPECT_CALL( object , connectSlot( Truly( PredicateFunc ) ) ).Times( 1 ).WillOnce(Return( boost::signals::connection() ) );

虽然你有一个函数(或函子)

bool PredicateFunc( boost::signal0<void>::slot_type const& slot )
{
    /* Custom matcher code */
    return true | false;
}

答案 1 :(得分:0)

要使用EXPECT_CALL,您的班级需要支持operator==。由于boost::program_options::variables_map没有operator==,因此您无法像这样使用它。

boost::program_options::variables_map你可以define you own matcher,但是我会建议你pass it to a function检查预期值(或者你可以忽略,设置标记或做任何你喜欢的事情)。像这样:

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

在测试中:

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