1

尝试执行此操作时,我遇到了 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

还有一些类似的错误。

4

2 回答 2

1

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

这里

在您的测试中:

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;
}
于 2012-01-27T12:47:20.553 回答
0

要使用 EXPECT_CALL,您的类需要支持operator==. 由于boost::program_options::variables_maphas no operator==,您不能那样使用它。

您可以为定义自己的匹配器boost::program_options::variables_map但是我建议您将其传递给一个检查预期值的函数(或者您可以忽略、设置标志或做任何您喜欢的事情)。像这样的东西:

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
于 2012-01-25T11:19:35.710 回答