如何在Google Mock中使用可选参数模拟方法? [英] How to mock method with optional parameter in Google Mock?
本文介绍了如何在Google Mock中使用可选参数模拟方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Google Mock 中模拟带有可选参数的方法? 例如:
How to mock a method with optional parameter in Google Mock? For example:
class A
{
public:
void set_enable( bool enabled = true );
};
class MockA : public A
{
MOCK_METHOD1( set_enable, void( bool ) ); // this is not working
};
推荐答案
这是Marko回答的替代方法:如果您不想更改原始代码,只需在模拟类中实现帮助程序即可:
This is an alternative of Marko's answer: If you don't want to change your original code, just implement the helper in the mock class:
class A
{
public:
virtual void set_enable( bool enabled = true );
};
class MockA : public A
{
MOCK_METHOD1( set_enable_impl, void( bool ) );
virtual void set_enable( bool enabled = true )
{
set_enable_impl( enabled );
{
};
例如,您仍然需要在测试中调用set_enable_impl
You still have to expect calls of set_enable_impl
in your tests, for example
MockA mockA;
EXPECT_CALL(mockA, set_enable_impl(true)).Times(Exactly(1));
EXPECT_CALL(mockA, set_enable_impl(false)).Times(Exactly(1));
这篇关于如何在Google Mock中使用可选参数模拟方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文