How to mock method with optional parameter in Google Mock?

前端 未结 3 761
醉酒成梦
醉酒成梦 2021-02-18 21:30

How to mock a method with optional parameter in Google Mock? For example:

class A
{ 
public:
    void set_enable( bool enabled = true );
};

cl         


        
3条回答
  •  梦谈多话
    2021-02-18 21:46

    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 );
        {
    };
    

    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));
    

提交回复
热议问题