How to mock a method with optional parameter in Google Mock? For example:
class A
{
public:
void set_enable( bool enabled = true );
};
cl
Change implementation of your method set_enable
to use a helper method, like this:
void set_enable( bool enabled = true ) { set_enable_impl(enabled); }
Now, in class MockA
, create a mock method for set_enable_impl
:
MOCK_METHOD1( set_enable_impl, void( bool ) );
Then, in your production code you simply use set_enable
as you would in the first place, while in tests you can set expectations on method set_enable_impl
:
MockA mockA;
EXPECT_CALL(mockA, set_enable_impl(_))...;
An alternative would be to overload the method by having versions with one and zero parameters. It is up to you to determine which way works better for your case.